# Proc and lambda

**URL:** <https://www.ruby-forum.com/t/proc-and-lambda/147213>\
**Category:** Ruby\
**Created:** [August 29, 2008, 12:57pm UTC](https://www.ruby-forum.com/t/proc-and-lambda/147213 "2008-08-29T12:57:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hunt\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9e5f3/32.png) [@Hunt\_J](https://www.ruby-forum.com/u/Hunt_J)\
**Post date:** [August 29, 2008, 12:57pm UTC](https://www.ruby-forum.com/t/proc-and-lambda/147213/1 "2008-08-29T12:57:23Z")

</div>

I’m new to Ruby with PHP background.

- Is there any super easy explanation or tutorial for proc and lamda?
- Do I need experience with Lisp or Smalltalk?

Proc and lambda are most fun part in ruby, people say. But, I feel I’m  
dumb. I read the Pickaxe book, but couldn’t fully understand the  
concept. I don’t have to be able to use them soon, but I at least need  
to comprehend them.

-J

---

<div class="post-metadata">

**Author:** ![Hunt\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9e5f3/32.png) [@Hunt\_J](https://www.ruby-forum.com/u/Hunt_J)\
**Post date:** [August 29, 2008, 1:07pm UTC](https://www.ruby-forum.com/t/proc-and-lambda/147213/2 "2008-08-29T13:07:57Z")

</div>

Procs are very surprising when no background in languages that  
implements it.

Actually concepts is very simple. It’s just some code chunk, like  
anonymous function that you can send inline. Concepts are very similar  
to javascript functions.

Here is an (unrealistic) example :

in javascipt :

el = Document.getElementById(‘blah’);  
el.onclick = function(event) {  
alert(‘Do something with the event’);  
}

In ruby (if navigators understood it…) :

el = Document.gel\_element\_by\_id(‘blah’);  
el.onclick = Proc.new{|event| puts “Do something with the event”}

Like in javacript, Proc.new create a anonymous function that would be  
executed only when the dom element is clicked.

Most of the time we don’t use Proc.new but directly pass a block to a  
function call. This is most of the time to iterate over a collection.

[1, 2, 3].each do |item|  
puts item  
end

=\> 1  
=\> 2  
=\> 3

For each element of the array the each function we pass the value to  
the block and let you do what you want with the item.

That was just a intro but HTH.

Antonin.

---

<div class="post-metadata">

**Author:** ![Hunt\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9e5f3/32.png) [@Hunt\_J](https://www.ruby-forum.com/u/Hunt_J)\
**Post date:** [August 29, 2008, 1:13pm UTC](https://www.ruby-forum.com/t/proc-and-lambda/147213/3 "2008-08-29T13:13:48Z")

</div>

On Fri, Aug 29, 2008 at 12:51 PM, Hunt J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’m new to Ruby with PHP background.
> 
> - Is there any super easy explanation or tutorial for proc and lamda?
> - Do I need experience with Lisp or Smalltalk?
> 
> Proc and lambda are most fun part in ruby, people say. But, I feel I’m  
> dumb. I read the Pickaxe book, but couldn’t fully understand the  
> concept. I don’t have to be able to use them soon, but I at least need  
> to comprehend them.

The easiest way for me to understand those are to think of them  
as anonymous methods, that can be stored in variables and called  
later on:

irb(main):001:0\> l = lambda {|x| puts x}  
=\> #Proc:0xb7c1c8cc@:1(irb)  
irb(main):002:0\> l.call(“hello”)  
hello  
=\> nil

An important characteristic is that they are closures. This means that  
they “trap” the scope in which they are defined, and have access to that  
scope when they are called, even though the call can be made in a  
different  
scope. This is better explained with an example:

irb(main):003:0\> class Test  
irb(main):004:1\> def execute (l)  
irb(main):005:2\> l.call(“test”)  
irb(main):006:2\> end  
irb(main):007:1\> end  
=\> nil  
irb(main):008:0\> x = “a variable”  
=\> “a variable”  
irb(main):009:0\> l = lambda {|var| puts “#{var} - #{x}”}  
=\> #Proc:0xb7bf74dc@:9(irb)  
irb(main):010:0\> Test.new.execute(l)  
test - a variable

As you can see, the lamdba block is executed in the scope of the  
method execute of class Test, where the variable x is not in scope.  
But, the block has access to x, cause it’s a closure.

Hope I didn’t mess the explanation…

There are some subtle differences between Proc, lambda and proc that  
I’m not too sure about, so I’ll leave that explanation to others.

Jesus.

---

<div class="post-metadata">

**Author:** ![Hunt\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9e5f3/32.png) [@Hunt\_J](https://www.ruby-forum.com/u/Hunt_J)\
**Post date:** [August 29, 2008, 1:14pm UTC](https://www.ruby-forum.com/t/proc-and-lambda/147213/4 "2008-08-29T13:14:58Z")

</div>

> - Is there any super easy explanation or tutorial for proc and lamda?

They are just functions, the main difference from PHP is that you can  
store  
these functions in variables and pass them to methods that can then call  
them. lambda() and Proc.new both create Proc objects, that are one of  
the  
various types of ‘callable object’ in Ruby.

For example

adder = lambda { |a,b| a + b }  
adder.call(3,4) #=\> returns 7

You can let methods accept a lambda (a code block) and call it:

def with\_two\_numbers(a, b, &block)  
block.call(a,b)  
end

with\_two\_numbers(3, 4, &adder) #=\> returns 7

See how we stored the adder function in a variable and passed a  
reference to  
it into another method, which could then call the adder function.

That’s basically all there is to it – creating functions and passing  
them  
around as data.

---

<div class="post-metadata">

**Author:** ![Hunt\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/b9e5f3/32.png) [@Hunt\_J](https://www.ruby-forum.com/u/Hunt_J)\
**Post date:** [August 30, 2008, 4:10am UTC](https://www.ruby-forum.com/t/proc-and-lambda/147213/5 "2008-08-30T04:10:07Z")

</div>

Hunt J. wrote:

> I’m new to Ruby with PHP background.
> 
> - Is there any super easy explanation or tutorial for proc and lamda?
> - Do I need experience with Lisp or Smalltalk?
> 
> Proc and lambda are most fun part in ruby, people say. But, I feel I’m  
> dumb. I read the Pickaxe book, but couldn’t fully understand the  
> concept. I don’t have to be able to use them soon, but I at least need  
> to comprehend them.
> 
> -J

This bothered me for the longest time and the clearest description I’ve  
encountered is in the O’Reilly book The Ruby P.ming Language.

“A proc is an object form of a block, and it behaves like a block. A  
lambda has slightly modified behavior and behaves more like a method  
than a block.” p. 197

Briefly, they respond differently to return and break. A return executed  
in a proc returns not only from the block but any enclosing methods as  
well. A return in a Lambda only returns from the lambda (i.e. the  
analogy to a method).

There’s more on breaks, next, etc in the book. The best coverage I’ve  
seen on intermediate and advanced Ruby concepts and very readable. Check  
it out…
