Create a gist now

Instantly share code, notes, and snippets.

Blocks, Procs and Lambda - Oh My!
def test_lambda
puts "About to call lambda"
blck = lambda do
puts " -- Start lambda"
return "lambda"
puts " -- End lambda"
end
val = blck.call
puts "The value is #{val}"
puts "Just called lambda"
end
test_lambda
def test_proc
puts "About to call proc"
blck = proc do
puts " -- Start proc"
return "proc"
puts " -- End proc"
end
val = blck.call
puts "The value is #{val}"
puts "Just called proc"
end
test_proc
def test_proc_object
puts "About to call Proc.new"
blck = Proc.new do
puts " -- Start Proc.new"
return "Proc.new"
puts " -- End Proc.new"
end
val = blck.call
puts "The value is #{val}"
puts "Just called Proc.new"
end
test_proc_object
# Try running on Ruby 1.8.7 and 1.9.3
puts "Blocks, Procs and Lambdas, Oh My!\n\n"
require "lambda"
puts "\n\n"
require "proc"
puts "\n\n"
require "proc_object"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment