<

Ruby Lambda

Published on
9,399 Points
3,399 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Join & Write a Comment

This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month