Skip to content

Instantly share code, notes, and snippets.

@chubas
Created October 10, 2012 02:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chubas/3862719 to your computer and use it in GitHub Desktop.
Save chubas/3862719 to your computer and use it in GitHub Desktop.
(Some) differences between ruby versions.
#!/bin/env ruby
# encoding: utf-8
# Differences between Ruby versions
code = case ARGV[0]
when "1"
puts "- Local block variables"
<<-CODE
i = 10
1.upto(5) do |i|
puts i
end
puts \"i is \#{i}\"
CODE
when "2"
puts "- Hash changes"
<<-CODE
hash = { 'dog' => 'bark', 'cat' => 'meow', 'bird' => 'tweet' }
puts hash.keys.inspect
puts hash.to_s
CODE
when "3"
puts "- String changes"
<<-CODE
puts "STRING"[0]
puts "eñe".size
CODE
when "4"
puts "- Syntax changes"
[
"
my_proc = -> x { puts x + 1 }
my_proc.call(10)
",
"
my_proc = proc { |x| puts x + 1 }
my_proc.(10)
",
"
hash = { foo: 'bar', baz: 'quux' }
puts hash
"
]
when "5"
puts "- Method changes"
[
"
def some_method(required, *optional, config)
puts \"Required: \#{required}\"
puts \"Optional: \#{optional}\"
puts \"Config: \#{config}\"
end
some_method(1, 2, 3, 4, :option_1 => 'x', :option_2 => 'y')
",
"
def some_method(first = \"first\", second)
puts [first, second].inspect
end
some_method(10)
"
]
end
code = [code] if code.is_a?(String)
code.each do |c|
puts c
$stdin.gets
begin
eval c
rescue SyntaxError => e
puts e
end
puts "----------"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment