From: david karapetyan Date: 2007-08-19T05:52:24+09:00 Subject: Re: method access to the enclosing method's locals? ------=_Part_113679_11164795.1187470348278 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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. On 8/18/07, David A. Black wrote: > > Hi -- > > This isn't quite the same as doing an inner def, because you're > defining #hi as a singleton method. If you want to define it as an > instance method of the enclosing class, you would want to do: > > self.class.class_eval do ... > > As for the head spinning, hopefully someday we'll have this method in > the core (and meanwhile you can write it yourself): > > class Object > def singleton_class > class << self; self; end > end > end > > Then you can do: > > singleton_class.class_eval do ... > > which is a little less cluttered. Also, keep in mind that it's not > really redirection; it's just sending messages to objects :-) The fact > that you can send messages to Class objects means that you can do what > feels like "meta" stuff in much the same way that you do other things. > > > 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) > > ------=_Part_113679_11164795.1187470348278--