Skip to content

Instantly share code, notes, and snippets.

@dgrijalva
Created November 19, 2009 22:26
Show Gist options
  • Save dgrijalva/239094 to your computer and use it in GitHub Desktop.
Save dgrijalva/239094 to your computer and use it in GitHub Desktop.
Ruby 1.8 -> 1.9 Forwards Compatibility

Forward Compatibility with Ruby 1.9

These gists are to support this blog post.

# Ruby 1.8
p({:a => '123', :b => '234'})
#=> {:b=>"234", :a=>"123"}
# Ruby 1.9
p({:a => '123', :b => '234'})
#=> {:a=>"123", :b=>"234"}
# Ruby 1.8
p Object.superclass
#=> nil
# Ruby 1.9
p Object.superclass
#=> BasicObject
# Ruby 1.8
a = 0
%w{all your base}.each{|a| puts a}
puts "a = #{a}"
#=> all
#=> your
#=> base
#=> a = base
# Ruby 1.9
a = 0
%w{all your base}.each{|a| puts a}
puts "a = #{a}"
#=> all
#=> your
#=> base
#=> a = 0
# Ruby 1.8
%w{a b c}.each do |w|
p w
a += 1
retry unless a > 5
end
#=> "a"
#=> "a"
#=> "a"
#=> "a"
#=> "a"
#=> "b"
#=> "c"
# Ruby 1.9
%w{a b c}.each do |w|
p w
a += 1
retry unless a > 5 # BOOM!
end
# Ruby 1.8
if a > 0 then a = 0 #okay
if a > 0 : a = 0 #okay
# Ruby 1.9
if a > 0 then a = 0 #okay
if a > 0 : a = 0 #not okay
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment