# Define\_method with dynamic method name

**URL:** <https://www.ruby-forum.com/t/define-method-with-dynamic-method-name/143754>\
**Category:** Ruby\
**Created:** [July 22, 2008, 2:10pm UTC](https://www.ruby-forum.com/t/define-method-with-dynamic-method-name/143754 "2008-07-22T14:10:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![balinski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f19dbf/32.png) [@balinski](https://www.ruby-forum.com/u/balinski)\
**Post date:** [July 22, 2008, 2:10pm UTC](https://www.ruby-forum.com/t/define-method-with-dynamic-method-name/143754/1 "2008-07-22T14:10:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![balinski](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/f19dbf/32.png) [@balinski](https://www.ruby-forum.com/u/balinski)\
**Post date:** [July 22, 2008, 2:16pm UTC](https://www.ruby-forum.com/t/define-method-with-dynamic-method-name/143754/2 "2008-07-22T14:16:30Z")

</div>

On Tuesday 22 July 2008, Marcin Balinski wrote:

> `method\_name’  
> 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  
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
