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 / trevoke.net>wrote:

> 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
> :)
> --
> Posted via http://www.ruby-forum.com/.
>
>