Define_method with dynamic method name

Hi
I want to define new method with dynamic name eg:

def method_name
  "abrakadabra"
end

define_method(method_name.to_sym){puts "done."}

I cant do it this way, it causes: undefined local variable or method
`method_name'
Is there any other way to achieve this?

···

--
Posted via http://www.ruby-forum.com/.

Are you using define method directly in the class body or from within an
instance method? In the first case (and also if you're using it from a class
method), then you need to define method_name as a class method, not as an
instance method:

class C

  def self.method_name
    "abrakadabra"
  end

  define_method(method_name){puts "done."}

end

C.new.abrakadabra

By the way, you don't need to call to_sym, since define_method also accepts a
string as argument.

I hope this helps

Stefano

···

On Tuesday 22 July 2008, Marcin Balinski wrote:

Hi
I want to define new method with dynamic name eg:

def method_name
  "abrakadabra"
end

define_method(method_name.to_sym){puts "done."}

I cant do it this way, it causes: undefined local variable or method
`method_name'
Is there any other way to achieve this?