Proc and lambda

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

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.

On Fri, Aug 29, 2008 at 12:51 PM, Hunt J. [email protected]
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:[email protected]: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:[email protected]: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.

  • 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.

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…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs