Proc and lambda

H

Hunt Jon

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
 
A

Antonin Amand

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

Jesús Gabriel y Galán

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@(irb):1>
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@(irb):9>
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.
 
J

James Coglan

[Note: parts of this message were removed to make it a legal post.]
* 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top