There are different ways to define a lambda or proc in ruby. [*]
-
with
lambda
keywordtest = 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:
- There is only
Proc
. Bothproc
andlambda
create instances ofProc
(but lambda variants do not allow omitting arguments which don't have a default value). -
Proc.new
exists, but callingproc
is the same asProc.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')
Posted by Judith Roth to makandra dev (2015-11-24 13:40)