Comprehensive Ruby Programming
上QQ阅读APP看书,第一时间看更新

Argument count

The first key difference is that lambdas count the arguments you pass to them, whereas procs do not. Consider this example:

full_name = lambda { |first, last| first + " " + last}  
p full_name.call("Jordan", "Hudgens")

Running this code will work properly. However, observe it when I pass another argument like this:

p full_name.call("Jordan", "David", "Hudgens") 

The application throws an error saying that we're passing in the wrong number of arguments:

Now, let's see what happens with procs:

full_name = Proc.new{ |first, last| first + " " + last}  
p full_name.call("Jordan", "David", "Hudgens")

If you run this code, you can see that it does not throw an error. It simply looks at the first two arguments and ignores anything after that.

In review, lambdas count the arguments passed to them, whereas procs don't.