Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - lambda vs. Proc.new [duplicate]

Possible Duplicate:
What's the difference between a proc and a lambda in Ruby?

When run this Ruby code:

def func_one     proc_new = Proc.new {return "123"}     proc_new.call     return "456" end  def func_two     lambda_new = lambda {return "123"}     lambda_new.call     return "456" end  puts "The result of running func_one is " + func_one puts "" puts "The result of running func_two is " + func_two 

The result that I get is as follows:

The result of running func_one is 123  The result of running func_two is 456 

As for func_two, where is the the value of the first return, that is, 123?

Thanks.

like image 470
Simplicity Avatar asked Aug 12 '11 16:08

Simplicity


People also ask

What is difference between lambda and Proc in Ruby?

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!" }

Why we use Proc in Ruby?

A Proc object is an encapsulation of a block of code, which can be stored in a local variable, passed to a method or another Proc, and can be called. Proc is an essential concept in Ruby and a core of its functional programming features.

How is a block different from a proc?

Procs are objects, blocks are not A proc (notice the lowercase p) is an instance of the Proc class. This lets us call methods on it and assign it to variables. Procs can also return themselves. In contrast, a block is just part of the syntax of a method call.

What is Ruby lambda?

Engineering Ruby. Lambdas are a powerful feature of the Ruby language. They allow you to wrap logic and data into a portable package. In this post, we'll cover how and when to use lambdas. You'll also learn about the difference between lambdas and Procs, and the performance profile of lambda functions.


2 Answers

This is one of the main differences between Procs and lambdas.

A return in a Proc returns from its enclosing block/method, while a return in a lambda simply returns from the lambda. When you call the lambda inside the func_two, it simply returns its value in place, which is not saved.

Read on Procs v. lambdas here: http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Method_Calls

See duplicate SO question: Why does explicit return make a difference in a Proc?

EDIT:

To further illustrate this difference, swap func_one and func_two for blocks and see what happens:

> begin; lambda { return 1 }.call end 1 > begin; Proc.new { return 1 }.call end LocalJumpError: unexpected return ... 
like image 90
numbers1311407 Avatar answered Sep 29 '22 12:09

numbers1311407


In the proc, the return "123" is bubbling up and returning from the outer function, func_one. Therefore the second return statement is never encountered.

In the lambda, the return "123" is returning only from the lambda. You're not setting an variable to the return value of the lambda (when you do lambda_new.call, so the value is basically just thrown out. Then, the return "456" is called and returns from the function. If, instead of returning "456", you returned lambda_new.call, the return value of func_two would be "123".

like image 24
Emily Avatar answered Sep 29 '22 11:09

Emily