Created
October 14, 2012 18:32
-
-
Save jeffyip/3889412 to your computer and use it in GitHub Desktop.
Ruby 1.9 Upgrade Guide Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts ('a'..'z').member?("airbnb") | |
# 1.8.7 > true | |
# 1.9.3 > false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.methods.include?("freeze") | |
# 1.8.7 > true | |
# 1.9.3 > false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.respond_to?(:freeze) | |
# 1.8.7 > true | |
# 1.9.3 > true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: UTF-8 | |
PUNCTUATION_CHARS = '!"#$%&\'()*+,-./:;<=>?@\[\]^_\`{|}~' | |
puts PUNCTUATION_CHARS.gsub(/[[:punct:]]/, "☃") | |
# 1.8.7 > ☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃☃ | |
# 1.9.3 > ☃☃☃$☃☃☃☃☃☃+☃☃☃☃☃☃<=>☃☃☃☃☃☃^☃☃`☃|☃~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
ruby 1.8
"a string"[:key] => nil
ruby 1.9
"a string"[:key] => exception