Skip to content

Instantly share code, notes, and snippets.

@jeffyip
Created October 14, 2012 18:32
Show Gist options
  • Save jeffyip/3889412 to your computer and use it in GitHub Desktop.
Save jeffyip/3889412 to your computer and use it in GitHub Desktop.
Ruby 1.9 Upgrade Guide Examples
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
@Gauravshah
Copy link

ruby 1.8
"a string"[:key] => nil
ruby 1.9
"a string"[:key] => exception

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment