Main Page | Report this Page
Computers Forum Index  »  Computer Languages (Ruby)  »  Closures / lambda question...
Page 1 of 1    

Closures / lambda question...

Author Message
Aldric Giacomoni...
Posted: Wed Oct 28, 2009 5:17 am
Guest
This is something I don't understand, and did not understand when I
studied LISP. I just watched Dave Thomas' presentation, "Extending Ruby
for Fun and Profit", which I by the way highly recommend to everyone who
hasn't seen it...
And he has the following example:

def proc_with_enclosing_scope
name = "Ruby"
lambda { puts name }
end

the_proc = proc_with_enclosing_scope
the_proc.call

name = "Java"
the_proc.call

_____

I don't understand what Ruby is doing / what happens.
First question: the "name" variable is defined inside the method
proc_with_enclosing_scope, so why would changing the name outside the
method make a difference in the first place?

Second question: I tried to type this straight into irb and made a small
typo, so it came out as such -

Quote:
def proc_with_enclosing_scope
name = "Ruby"
lamda { puts name }
end
=> nil
the_proc = proc_with_enclosing_scope
NoMethodError: undefined method `lamda' for main:Object

from (irb):23:in `proc_with_enclosing_scope'
from (irb):25

So.... When the_proc gets assigned the ... Value of the method
running... (?) What does it get assigned?

And lastly.. I know that "proc" exists, too. What is the difference /
what does it do?

I thank you very much in advance for the enlightenment you will provide
Smile
--
Posted via http://www.ruby-forum.com/.
 
Aldric Giacomoni...
Posted: Wed Oct 28, 2009 5:17 am
Guest
Zundra Daniel wrote:
Quote:
I think what Dave was trying to illustrate with this example is that
even
though the method that you're calling has gone out of scope that it
still
retains the value of the variable defined inside.

Whats fascinating about this simple example is the fact that the method
which is assigned to a remembers the context of everything defined
internally. Think about that for a second. In languages that do not
support closures the values of the properties defined are lost until the
next time the method is called, however closures not only remember the
values of their properties after the function goes out of scope but the
context in which they were originally defined. Its a pretty simple
concept
but a very powerful one at the same time.

Sounds like I was trying to overcomplicate it, when it's just blindingly
obvious.. Okay. I'll try to play with that and see how far I can take
it. Thanks Smile
--
Posted via http://www.ruby-forum.com/.
 
Aldric Giacomoni...
Posted: Wed Oct 28, 2009 5:17 am
Guest
Marnen Laibow-Koser wrote:
Quote:
Aldric Giacomoni wrote:

def proc_with_enclosing_scope
name = "Ruby"
lambda { puts name }
end

the_proc = proc_with_enclosing_scope
the_proc.call

name = "Java"
the_proc.call

why would changing the name outside the
method make a difference in the first place?

It doesn't. Did you try the code? Both instances of the_proc.call
return "Ruby". I assume the 'name="Java"' line is just there to point
out that it's not the same variable.

I did try the code. I was confused by what Dave Thomas was trying to
point out, it seemed obvious to me that the code would return Ruby both
times.

Quote:

So.... When the_proc gets assigned the ... Value of the method
running... (?) What does it get assigned?

proc_with_enclosing_scope.class
=> Proc


And lastly.. I know that "proc" exists, too. What is the difference /
what does it do?

lambda {} is nearly equivalent to Proc.new {}. See
http://ruby-doc.org/core/classes/Kernel.html#M005945 .

Alright.. I had read that before and hadn't made too much sense of it,
but maybe I'm just thinking too hard.
So.. Proc and Lambda are .. Pretty much the same thing, according to
Ruby.

So they really are "bits of code with its own environment" ?
When is that useful? Maybe I need to watch Dave Thomas' presentation
again.. :)

Thanks, Marnen.
--
Posted via http://www.ruby-forum.com/.
 
Zundra Daniel...
Posted: Wed Oct 28, 2009 5:17 am
Guest
[Note: parts of this message were removed to make it a legal post.]

I'd actually like to know the answer to that one. What is the reason for
enclosing the rspec test case in a lamda? Is it because you don't want the
exception actually raised in the test case but raised in another context
then observed?

On Wed, Oct 28, 2009 at 12:36 AM, Aldric Giacomoni <aldric at (no spam) trevoke.net>wrote:

Quote:
Aldric Giacomoni wrote:
Marnen Laibow-Koser wrote:

In Rails they're occasionally used for callbacks, and in
RSpec I often do things like
lambda {Car.drive}.should_not raise_error

See http://en.wikipedia.org/wiki/Closure_(computer_science)<http://en.wikipedia.org/wiki/Closure_%28computer_science%29>for more.

I'm reading that wiki page right now Wink
About that RSpec example..

You are creating "code which calls Car.drive", yes ? Why not just call
Car.drive ? That is the main bit of thinking I'm having issues with.

Never mind!
I just found this : http://innig.net/software/ruby/closures-in-ruby.rb
Enlightening. Thanks again for your help, it makes it all easier to
read.
--
Posted via http://www.ruby-forum.com/.

 
Marnen Laibow-Koser...
Posted: Wed Oct 28, 2009 5:17 am
Guest
Aldric Giacomoni wrote:
[...]
Quote:
So.. Proc and Lambda are .. Pretty much the same thing, according to
Ruby.

Not quite. There's no such class as Lambda. Read what I said in my
first post again.

Quote:

So they really are "bits of code with its own environment" ?
When is that useful? Maybe I need to watch Dave Thomas' presentation
again.. Smile

Closures are useful for certain things and in certain styles of
programming. In Rails they're occasionally used for callbacks, and in
RSpec I often do things like
lambda {Car.drive}.should_not raise_error

See http://en.wikipedia.org/wiki/Closure_(computer_science) for more.

Quote:

Thanks, Marnen.

You're welcome!

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen at (no spam) marnen.org
--
Posted via http://www.ruby-forum.com/.
 
Aldric Giacomoni...
Posted: Wed Oct 28, 2009 5:17 am
Guest
Marnen Laibow-Koser wrote:

Quote:
Not quite. There's no such class as Lambda. Read what I said in my
first post again.

I begin to understand. I'm reading this :
http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/
And it explains it as well.
Quote:

In Rails they're occasionally used for callbacks, and in
RSpec I often do things like
lambda {Car.drive}.should_not raise_error

See http://en.wikipedia.org/wiki/Closure_(computer_science) for more.

I'm reading that wiki page right now Wink
About that RSpec example..

You are creating "code which calls Car.drive", yes ? Why not just call
Car.drive ? That is the main bit of thinking I'm having issues with.
--
Posted via http://www.ruby-forum.com/.
 
Zundra Daniel...
Posted: Wed Oct 28, 2009 5:17 am
Guest
[Note: parts of this message were removed to make it a legal post.]

I think what Dave was trying to illustrate with this example is that even
though the method that you're calling has gone out of scope that it still
retains the value of the variable defined inside.

Whats fascinating about this simple example is the fact that the method
which is assigned to a remembers the context of everything defined
internally. Think about that for a second. In languages that do not
support closures the values of the properties defined are lost until the
next time the method is called, however closures not only remember the
values of their properties after the function goes out of scope but the
context in which they were originally defined. Its a pretty simple concept
but a very powerful one at the same time.

On Tue, Oct 27, 2009 at 11:24 PM, Aldric Giacomoni <aldric at (no spam) trevoke.net>wrote:

Quote:
This is something I don't understand, and did not understand when I
studied LISP. I just watched Dave Thomas' presentation, "Extending Ruby
for Fun and Profit", which I by the way highly recommend to everyone who
hasn't seen it...
And he has the following example:

def proc_with_enclosing_scope
name = "Ruby"
lambda { puts name }
end

the_proc = proc_with_enclosing_scope
the_proc.call

name = "Java"
the_proc.call

_____

I don't understand what Ruby is doing / what happens.
First question: the "name" variable is defined inside the method
proc_with_enclosing_scope, so why would changing the name outside the
method make a difference in the first place?

Second question: I tried to type this straight into irb and made a small
typo, so it came out as such -

def proc_with_enclosing_scope
name = "Ruby"
lamda { puts name }
end
=> nil
the_proc = proc_with_enclosing_scope
NoMethodError: undefined method `lamda' for main:Object
from (irb):23:in `proc_with_enclosing_scope'
from (irb):25

So.... When the_proc gets assigned the ... Value of the method
running... (?) What does it get assigned?

And lastly.. I know that "proc" exists, too. What is the difference /
what does it do?

I thank you very much in advance for the enlightenment you will provide
Smile
--
Posted via http://www.ruby-forum.com/.

 
Marnen Laibow-Koser...
Posted: Wed Oct 28, 2009 5:17 am
Guest
Aldric Giacomoni wrote:
Quote:
This is something I don't understand, and did not understand when I
studied LISP. I just watched Dave Thomas' presentation, "Extending Ruby
for Fun and Profit", which I by the way highly recommend to everyone who
hasn't seen it...
And he has the following example:

def proc_with_enclosing_scope
name = "Ruby"
lambda { puts name }
end

the_proc = proc_with_enclosing_scope
the_proc.call

name = "Java"
the_proc.call

_____

I don't understand what Ruby is doing / what happens.
First question: the "name" variable is defined inside the method
proc_with_enclosing_scope, so why would changing the name outside the
method make a difference in the first place?

It doesn't. Did you try the code? Both instances of the_proc.call
return "Ruby". I assume the 'name="Java"' line is just there to point
out that it's not the same variable.

Quote:

Second question: I tried to type this straight into irb and made a small
typo, so it came out as such -

def proc_with_enclosing_scope
name = "Ruby"
lamda { puts name }
end
=> nil
the_proc = proc_with_enclosing_scope
NoMethodError: undefined method `lamda' for main:Object
from (irb):23:in `proc_with_enclosing_scope'
from (irb):25

So.... When the_proc gets assigned the ... Value of the method
running... (?) What does it get assigned?

proc_with_enclosing_scope.class
=> Proc


Quote:

And lastly.. I know that "proc" exists, too. What is the difference /
what does it do?

lambda {} is nearly equivalent to Proc.new {}. See
http://ruby-doc.org/core/classes/Kernel.html#M005945 .

Quote:

I thank you very much in advance for the enlightenment you will provide
Smile

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen at (no spam) marnen.org
--
Posted via http://www.ruby-forum.com/.
 
Aldric Giacomoni...
Posted: Wed Oct 28, 2009 5:17 am
Guest
Aldric Giacomoni wrote:
Quote:
Marnen Laibow-Koser wrote:

In Rails they're occasionally used for callbacks, and in
RSpec I often do things like
lambda {Car.drive}.should_not raise_error

See http://en.wikipedia.org/wiki/Closure_(computer_science) for more.

I'm reading that wiki page right now Wink
About that RSpec example..

You are creating "code which calls Car.drive", yes ? Why not just call
Car.drive ? That is the main bit of thinking I'm having issues with.

Never mind!
I just found this : http://innig.net/software/ruby/closures-in-ruby.rb
Enlightening. Thanks again for your help, it makes it all easier to
read.
--
Posted via http://www.ruby-forum.com/.
 
 
Page 1 of 1    
All times are GMT
The time now is Tue Dec 01, 2009 9:43 am