Provide different number of parameters for lambda : lambda « Method « Ruby






Provide different number of parameters for lambda


l = lambda {|x,y| print x,y }
l.call(1,2)     # This works
l.call(1)       # Wrong number of arguments
l.call(1,2,3)   # Wrong number of arguments
l.call([1,2])   # Wrong number of arguments
l.call(*[1,2])  # Works: explicit splat to unpack the array

 








Related examples in the same category

1.It's possible to store code blocks within variables, using the lambda method
2.Use lambda to create a block
3.Use lambda to create a list of function
4.Invoking Blocks
5.return a lambda function