<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Ruby Lambda

Published on
9,455 Points
3,455 Views
Last Modified:
Approved
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between the two in order to give you a better idea of what they are in the hope that if you know a bit more about them you will begin to understand their respective uses. My feeling is that if you don't understand what you're doing or what you're using, you won't code as efficiently or as effectively.

Ruby contains these neat little things called a Lambda. It's actually a borrowed concept and name from Python, a language that has a lot of influence in Ruby's design.

These Lambda functions have different names in other languages, such as:

Lambda
Anonymous Function
Closure
They essentially name the same concept, so if you're coming from another language that has these, you already have a head start.

In Ruby, a Lambda is defined as follows,
x = lambda { return "ar har har har" }

Open in new window

If you're not new to Ruby, you've probably heard of procs. These are very similar to lambdas with very subtle, yet significant, differences. The most notable difference between a proc and a lambda is that returning from a proc will actually return from the method its defined within.  Example:
def a_meth
   proc = Proc.new{ return "blah" }
   proc.call
   return "not reached"
end

Open in new window


If you run this in IRB, you'll get "blah" returned instead of "not reached".  Now let's look at a similar method using a lambda:
def a_new_meth
   lam = lambda{ return "yeeee hah" }
   lam.call
   return "not the lambda"
end

Open in new window

The output is "not the lambda"; that is, unlike proc.call, lam.call does not force an immediate exit from the enclosing block.

As you can see, lambdas are real anonymous functions whereas procs are just self contained processes that can be run. Thus, if you want the result returned from the lambda, you would need to either use puts for output or store the result in a variable for later use.

Another note about lambdas and arity (arguments), they should be treated as if they are a function.  If you define this lambda:
 > x = lambda { |x, y| puts x + y } 

Open in new window

and call it like so,
 > x.call(1, 2, 3) 

Open in new window

you will get a "wrong number of arguments" error.

A proc will not throw that error.  It will simply accept the first two arguments, run the function according to them, and ignore the extras.  For example:
 p = Proc.new{|x,y| puts x + y}
p.call(1,2,3)

Open in new window

That'll return the expected number 3, which is the sum of the first two arguments passed into the function

Thanks for reading and good luck in your ruby ninjutsu adventures :)
0
Comment
Author:narshlob
0 Comments

Featured Post

JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Join & Write a Comment

This is Part-2 of Learning to use the Power of Mailwasher Pro so if you haven't watched Part-1 yet, I urge you to do so before watching this video. Click this link to watch Part-1 (https://www.experts-exchange.com/videos/56638/Learn-to-use-the-POWER…
Watch this simple and effective video tutorial to extract attachments from Outlook 2007 and try this easy method by yourself. No need to go anywhere, just watch the video and export attachments from Outlook in few simple steps. To know more, click h…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month