Define_method with dynamic method name

I want to define new method with dynamic name eg:

def method_name

define_method(method_name.to_sym){puts “done.”}

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

On Tuesday 22 July 2008, Marcin Balinski wrote:

Is there any other way to achieve this?

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
method), then you need to define method_name as a class method, not as
instance method:

class C

def self.method_name

define_method(method_name){puts “done.”}


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

I hope this helps


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs