From: "David A. Black" Date: 2007-08-19T06:59:30+09:00 Subject: Re: method access to the enclosing method's locals? Hi -- On Sun, 19 Aug 2007, david karapetyan wrote: > Maybe you can answer another question. While trying to figure this out I > kept running into scoping issues. The first thing that popped into my head > was > > def go2(arg) > clos = proc {puts "Hi #{arg}"} > class << self > define_method(:hi,clos) > end > end > > but this didn't work because for some reason when I'm inside the scope of > class << self ... end define_method can't find clos even though it is inside > the enclosing scope. Is there a reason classes don't close over their > enclosing scope because I was thinking just like we can have closures we > could also have closures for classes. The class keyword always starts a new local scope, but class_eval doesn't: x = 1 Object.class_eval { puts x } # 1 class/class_eval is analogous to def/define_method. class and def start new local scopes; class_eval and define_method don't. David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)