Created
February 13, 2010 03:33
-
-
Save beccasaurus/303236 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
$a_lambda = lambda { return } | |
def method_with_a_lambda | |
puts "lambda START" | |
begin | |
$a_lambda.call | |
rescue Exception => ex | |
puts " raised exception: #{ex}" | |
return | |
end | |
puts "lambda END" | |
end | |
$a_proc = proc { return } | |
def method_with_a_proc | |
puts "proc START" | |
begin | |
$a_proc.call | |
rescue Exception => ex | |
puts " raised exception: #{ex}" | |
return | |
end | |
puts "proc END" | |
end | |
$a_Proc_new = Proc.new { return } | |
def method_with_a_Proc_new | |
puts "Proc.new START" | |
begin | |
$a_Proc_new.call | |
rescue Exception => ex | |
puts " raised exception: #{ex}" | |
return | |
end | |
puts "Proc.new END" | |
end | |
if RUBY_VERSION.start_with?('1.9') | |
eval("$a_shorthand_lambda = -> { return }") | |
def method_with_a_shorthand_lambda | |
puts "-> START" | |
begin | |
$a_shorthand_lambda.call | |
rescue Exception => ex | |
puts " raised exception: #{ex}" | |
return | |
end | |
puts "-> END" | |
end | |
end | |
def method_with_a_block &block | |
puts "block START" | |
begin | |
block.call | |
rescue Exception => ex | |
puts " raised exception: #{ex}" | |
return | |
end | |
puts "block END" | |
end | |
puts "START" | |
method_with_a_proc | |
method_with_a_lambda | |
method_with_a_Proc_new | |
method_with_a_block { return } | |
method_with_a_shorthand_lambda if RUBY_VERSION.start_with?('1.9') | |
puts "END" |
This file contains hidden or 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
$ ruby1.8 proc-test.rb | |
START | |
method_with_a_proc START | |
method_with_a_proc END | |
method_with_a_lambda START | |
method_with_a_lambda END | |
method_with_a_Proc_new START | |
raised exception: unexpected return | |
END |
This file contains hidden or 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
$ ruby1.9 proc-test.rb | |
START | |
method_with_a_proc START | |
raised exception: unexpected return | |
method_with_a_lambda START | |
method_with_a_lambda END | |
method_with_a_Proc_new START | |
raised exception: unexpected return | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment