From: dblack@... Date: 2007-08-02T21:24:53+09:00 Subject: Re: defining methods dynamically? Hi -- On Thu, 2 Aug 2007, Wayne E. Seguin wrote: > On Aug 01, 2007, at 21:49 , dblack@rubypal.com wrote: >> >> There's actually no other way to do it :-) But I think what you're >> looking for is #define_method, which lets you define methods with >> dynamically-determined names and also gives you a way to flatten the >> variable scope of a method definition. >> > > David, > > I do not follow what you mean by "flatten the variable scope of a method > definition". > Could you please explain what this means a bit? Sure. Let's say you want to do: a = 1 class C def show_a a # won't work end end The definition of a won't make it into the scope of the method definition #show_a, because both the class keyword and the def keyword start a new local scope. However, class_eval and define_method are both permeable, as to scope. So you can do: a = 1 class C; end C.class_eval { define_method(:show_a) { a } } C.new.show_a # 1 The a inside the block given to define_method is the same a that I defined in the first line. That's what I mean by flattening the variable scope. 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)