Hi -- On Sat, 22 Apr 2006, James Edward Gray II wrote: > 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. To be fair to Sean, though, the question really had to do with clarifying how the scope of class_eval worked -- so removing it kind of defeats that particular purpose :-) David -- David A. Black (dblack / wobblini.net) Ruby Power and Light, LLC (http://www.rubypowerandlight.com) "Ruby for Rails" PDF now on sale! http://www.manning.com/black Paper version coming in early May!