Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby lambda arguments

Tags:

syntax

ruby

This code works as expected (does nothing, even doesn't produce warning/errors):

l = lambda {|i|}
l.call(1)

This code produces warning (warning: multiple values for a block parameter (0 for 1)):

l = lambda {|i|}
l.call

And this code fails with error (ArgumentError: wrong number of arguments (0 for 2)):

l = lambda {|i, y|}
l.call

I thought that lambda requires all argument to be passed.

And from the second example I see that it isn't. Why does it work when only one argument is given, and works as expected (fails with error) with more than one argument?

PS: ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]

UPDATE: I've checked these samples with ruby 1.9.1p376. And it works as expected - the second example also produces an error. Looks like this is a feature of 1.8 version (or <=1.8)

like image 678
andrii Avatar asked Feb 09 '10 20:02

andrii


People also ask

How do you use lambda in Ruby?

Ruby lambda In Ruby, a lambda is an object similar to a proc. Unlike a proc, a lambda requires a specific number of arguments passed to it, and it return s to its calling method rather than returning immediately. proc_demo = Proc. new { return "Only I print!" }

Does lambda support Ruby?

You can now develop AWS Lambda functions using Ruby 2.7. This is the latest release of Ruby and supports new features like pattern matching, argument forwarding and numbered arguments. Lambda functions written in Ruby 2.7 run on Amazon Linux 2, the latest generation of Amazon Linux.

What is the difference between block proc and lambda in Ruby?

When using parameters prefixed with ampersands, passing a block to a method results in a proc in the method's context. Procs behave like blocks, but they can be stored in a variable. Lambdas are procs that behave like methods, meaning they enforce arity and return as methods instead of in their parent scope.

What is -> in Ruby?

-> == Dash Rocket Used to define a lambda literal in Ruby 1.9.


2 Answers

Lambdas are weird like that, their behavior is different when you have less than two arguments. Check this article for more information.

like image 50
Firas Assaad Avatar answered Oct 20 '22 02:10

Firas Assaad


This script will teach you everything you need to know about closures in Ruby.

# So, what's the final verdict on those 7 closure-like entities?          
#
#                                                     "return" returns from closure
#                                    True closure?    or declaring context...?         Arity check?
#                                    ---------------  -----------------------------    -------------------
# 1. block (called with yield)       N                declaring                        no
# 2. block (&b => f(&b) => yield)    N                declaring                        no
# 3. block (&b => b.call)            Y except return  declaring                        warn on too few
# 4. Proc.new                        Y except return  declaring                        warn on too few
# 5. proc                                    <<< alias for lambda in 1.8, Proc.new in 1.9 >>>
# 6. lambda                          Y                closure                          yes, except arity 1
# 7. method                          Y                closure                          yes
like image 43
Trevoke Avatar answered Oct 20 '22 03:10

Trevoke