Is there reason why lambda is called lambda and proc is called proc?
Let's see what happened with call to proc
, and lambda
in ruby 1.8.7:
aa = lambda {|a| nil }
# => #<Proc:0xb7351850@(irb):6>
aa.call
# warning: multiple values for a block parameter (0 for 1)
# => nil
aa = proc {|a| nil }
# => #<Proc:0xb73451cc@(irb):10>
aa.call
# warning: multiple values for a block parameter (0 for 1)
As we can see, there is no difference between the proc
, and lambda
in Ruby. I believe that the proc
had appeared in Ruby
at first. Because it just alias to Proc.new
, which creates the Proc
object. Then the lambda
had added into ruby, because the Lambda is the so called anonymous function in computer programming area, and the developers could see in the language well-known name for that fucntion class.
Let's see that happened with the operators in ruby 1.9.1:
aa = lambda {|a| nil }
# => #<Proc:0x8054340@(irb):1 (lambda)>
aa.call
# ArgumentError: wrong number of arguments (0 for 1)
from (irb):2:in `call'
from (irb):2
from /home/malo/.rvm/rubies/ruby-1.9.1-p431/bin/irb:12:in `<main>'
aa = proc {|a| nil }
# => #<Proc:0x8319bf0@(irb):3>
aa.call
# => nil
As we can see, in ruby 1.9.2 (I guess since ruby 1.9) was added attribute lambda into Proc
instance saying that the anonymouns function will not accept the wrong argument amount. So passing the 0 arguments into 1 required raises the ArgumentError
exception. While the passing the 0 arguments into 1 required for the proc
object silently drops the unnecessary argument.
As you know from ruby 1.9.1 was added ->
as an alias to argumentless lambda
operator:
aa = -> { nil }
# => #<Proc:0x8056ffc@(irb):1 (lambda)>
aa = -> {|a| nil }
# SyntaxError: (irb):2: syntax error, unexpected '|'
aa = -> {|a| nil }
^