Aldric Giacomoni 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?

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.

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

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

> 
> I thank you very much in advance for the enlightenment you will provide
> :)

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen / marnen.org
-- 
Posted via http://www.ruby-forum.com/.