Hi --

On Sun, 15 Oct 2006, John Ky wrote:

>>>>>> 
> def define_my_method(method_name)
> define_method method_name do
>   yield
> end
> end
>
> class X
> define_my_method :method1 do
>   puts "Method 1"
> end
> define_my_method :method2 do
>   puts "Method 2"
>   method1
> end
> end
>
> X.new.method2
> <<<<<
>
> Gives:
>
> C:\wrk\johnk\wiksprint-001>define.rb
> Method 2
> C:/wrk/johnk/wiksprint-001/define.rb:13: undefined local variable or method 
> `met
> hod1' for X:Class (NameError)
>       from C:/wrk/johnk/wiksprint-001/define.rb:3:in `method2'
>       from C:/wrk/johnk/wiksprint-001/define.rb:17

The problem is that define_method creates instance methods -- in this
case, method1 and method2.  In method2 you call method1... but you're
calling it on the class object X, when you've defined it for
*instances* of X.

If you replace method1 with new.method1, in the second call to
define_my_method, you'll see the difference.


David

-- 
                   David A. Black | dblack / wobblini.net
Author of "Ruby for Rails"   [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog)        [2] | Co-director, Ruby Central, Inc.   [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com    | [4] http://www.rubycentral.org