On Apr 22, 2006, at 6:52 AM, Sean O'Halpin wrote:

> On 4/22/06, Pat Maddox <pergesu / gmail.com> wrote:
>
>> Can someone explain to me what the difference is between the inline
>> code example, and specifying it inside the class?
>
> A class definition introduces a new scope (similar to def) so for your
> example to work you need to define the local variable ~within~ the
> class definition for it to be visible from the closure, e.g.
>
>   class A
>     var = "initialized variable"
>     class_eval { define_method(:talk) { puts var } }
>   end
>
>   A.new.talk
>
>   #=> initialized variable

We might as well drop the class_eval { ... }, since we are already in  
the class:

 >> class A
 >>   var = "initialized variable"
 >>   define_method(:talk) { puts var }
 >> end
=> #<Proc:0x0033307c@(irb):3>
 >> A.new.talk
initialized variable
=> nil

I believe this to be an example of Dave Burt's new refactoring,  
Remove Unused Scope.

James Edward Gray II