Skip to content

Instantly share code, notes, and snippets.

@beccasaurus
Created February 13, 2010 03:33
Show Gist options
  • Save beccasaurus/303236 to your computer and use it in GitHub Desktop.
Save beccasaurus/303236 to your computer and use it in GitHub Desktop.
$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"
$ 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
$ 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