There are different ways to define a lambda or proc in ruby. [*]
with lambda
keyword
test = lambda do |arg|
puts arg
end
with the lambda literal ->
(since Ruby 1.9.1)
test = ->(arg) do
puts arg
end
with the proc
keyword (which defines a lambda that does not test the given number of arguments):
test = proc do |arg|
puts arg
end
[*] Technicalities:
Proc
. Both proc
and lambda
create instances of Proc
(but lambda variants do not allow omitting arguments which don't have a default value).Proc.new
exists, but
calling proc
is the same as Proc.new
Show archive.org snapshot
.And there are different ways to call them:
call
(we prefer this)
test.call('hello world')
Square brackets (could easily be mistaken for a hash)
test['hello world']
.()
(weird, isn't it?)
test.('hello world')