Created
December 14, 2011 02:48
-
-
Save blowmage/1475026 to your computer and use it in GitHub Desktop.
Blocks, Procs and Lambda - Oh My!
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
# 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