From: david karapetyan Date: 2007-08-18T17:42:15+09:00 Subject: Re: method access to the enclosing method's locals? ------=_Part_111586_5591340.1187426539266 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline Ok, just ignore all my previous posts. I should have done some more research before I started typing. This code will actually work. def go2(arg) (class << self; self; end).class_eval do define_method(:hi) do puts "Hi #{arg}" end end end Once you call go2 with some argument it will create the method hi that will have access to any argument passed to go2. I have to say though all the redirection with the singleton class makes my head spin a little. On 8/17/07, Larry Kluger wrote: > > > Hi, > > How can a method gain access to the enclosing method's locals? > > def go2(arg) > def hi > puts "Hi #{arg}!" > end > > a={:h => :hi} > send(a[:h]) > end > > go2 'Larry' ==>> NameError: undefined local variable or method `arg' for > main:Object > > In go2, is there a clean way for the hi method to have access to the > arg local? > > Thanks! > > Larry > -- > Posted via http://www.ruby-forum.com/. > > ------=_Part_111586_5591340.1187426539266--