Skip to content

Instantly share code, notes, and snippets.

@bf4
Forked from jeffyip/case.rb
Created June 28, 2013 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bf4/5888403 to your computer and use it in GitHub Desktop.
Save bf4/5888403 to your computer and use it in GitHub Desktop.
class Cocktail
attr_accessor :name
def initialize(name)
@name = name
end
def liquor
case self.name
when "journalist": "gin"
when "sazerac": "whiskey"
when "mojito": "rum"
end
end
end
cocktail = Cocktail.new("sazerac")
puts cocktail.liquor
# 1.8.7 > whiskey
# 1.9.3 > syntax error, unexpected ':', expecting keyword_then or ',' or ';' or '\n'
require 'rubygems'
require 'date'
Date.parse("10/13/2012").to_s
# 1.8.7 > "2012-10-13"
# 1.9.3 > ArgumentError: invalid date
Date.parse("10/11/2012").to_s
# 1.8.7 > "2012-10-11"
# 1.9.3 > "2012-11-10"
# encoding: utf-8
puts ('a'..'z').member?("airbnb")
# 1.8.7 > true
# 1.9.3 > false
String.methods.include?("freeze")
# 1.8.7 > true
# 1.9.3 > false
String.respond_to?(:freeze)
# 1.8.7 > true
# 1.9.3 > true
# encoding: UTF-8
PUNCTUATION_CHARS = '!"#$%&\'()*+,-./:;<=>?@\[\]^_\`{|}~'
puts PUNCTUATION_CHARS.gsub(/[[:punct:]]/, "☃")
# 1.8.7 > ☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃
# 1.9.3 > ☃☃☃$☃☃☃☃☃☃+☃☃☃☃☃☃<=>☃☃☃☃☃☃^☃☃`☃|☃~
# encoding: UTF-8
puts "1: " + "Hello Chloë,".gsub(/[\w]*,/u, '')
puts "2: " + "Hello Chloë,".gsub(/[[:alnum:]]*,/u, '')
puts "3: " + "Hello Chloë,".gsub(/[\w[:alnum:]]*,/u, '')
# ruby 1.8.7 output:
# 1: Hello
# 2: Hello Chloë
# 3: Hello
# ruby 1.9.3 output:
# 1: Hello Chloë
# 2: Hello
# 3: Hello
h = {1 => "a", 2 => "b", 3 => "c"}
puts h.select{|x,y| true}.inspect
puts Hash[h.select{|x,y| true}].inspect
# 1.8.7 output:
# [[1, "a"], [2, "b"], [3, "c"]]
# {1=>"a", 2=>"b", 3=>"c"}
# 1.9.3 output:
# {1=>"a", 2=>"b", 3=>"c"}
# {1=>"a", 2=>"b", 3=>"c"}
h = {"foo", "bar"}
puts h.inspect
# 1.8.7 > {"foo"=>"bar"}
# 1.9.3 > syntax error, unexpected ',', expecting tASSOC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment