Ruby Notes
Lambdas vs Procs
Both are proc objects
l = lambda {|name| puts "hello #{name}"}
p = proc {|name| puts "hello #{name}"}
l # => #<Proc:0x007ff16389da58@-:1 (lambda)>
p # => #<Proc:0x007ff16389da30@-:2>
Return behaves differently
lambda returns from the context of lambda only.
def do_it
l = lambda do |name|
puts "Hello #{name}"
return
end
l.call("World!")
puts "Bye World"
end
do_it
# >> Hello World!
# >> Bye World
Proc returns from the calling context too. - Note that the bye world is not printed.
def do_it
l = proc do |name|
puts "Hello #{name}"
return
end
l.call("World!")
puts "Bye World"
end
do_it
# >> Hello World!
Source