Lambda Proc Literal
The Lambda Proc Literal (->
) is used to create an anonymous, callable procedure.
> say_hello = -> { puts "Hello" }
=> #<Proc:0x00007fdb44addb18 (irb):78 (lambda)>
> say_hello.call
Hello
=> nil
> say_hello = ->(name) { puts "Hello, #{name}" }
=> #<Proc:0x00007fdb428d6088 (irb):81 (lambda)>
> say_hello.call("friend")
Hello, friend
=> nil
With the help of the block conversation operator (&
), we can convert a proc into a block for a method that takes a block argument.
> [1,2,3,4].map(&->(x) { x * x })
=> [1, 4, 9, 16]