Hi --

On Sat, 18 Aug 2007, 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?

def always starts a new local scope. If you want to define a method
but in the same scope, you need to use define_method, probably in
conjunction with class_eval or module_eval on the class or module you
want to put it in.


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)