# Define\_method

**URL:** <https://www.ruby-forum.com/t/define-method/119460>\
**Category:** Ruby\
**Created:** [November 7, 2007, 5:38am UTC](https://www.ruby-forum.com/t/define-method/119460 "2007-11-07T05:38:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Malcolm\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/54ee81/32.png) [@Malcolm\_L](https://www.ruby-forum.com/u/Malcolm_L)\
**Post date:** [November 7, 2007, 5:38am UTC](https://www.ruby-forum.com/t/define-method/119460/1 "2007-11-07T05:38:46Z")

</div>

Hi Guys,

I am trying to use define\_method like:

class TestClass  
def initialize  
define\_method(“hello”) { || puts “Hello” }  
end  
end

x = TestClass.new  
x.hello

I am always getting:  
NoMethodError: undefined method `define\_method’

Do I need to require something? Am I doing something wrong, or am I  
completely missing the boat!?

Any help appreciated!

Thanks,  
Malcolm.

---

<div class="post-metadata">

**Author:** ![Malcolm\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/54ee81/32.png) [@Malcolm\_L](https://www.ruby-forum.com/u/Malcolm_L)\
**Post date:** [November 7, 2007, 8:08am UTC](https://www.ruby-forum.com/t/define-method/119460/2 "2007-11-07T08:08:58Z")

</div>

On Nov 6, 11:37 pm, “Malcolm L.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi Guys,
> 
> I am trying to use define\_method like:

def selfclass  
(class \<\< self; self; end)  
end

class TestClass  
def initialize  
selfclass.send(:define\_method, “hello”) { || puts “Hello” }  
end  
end

x = TestClass.new  
x.hello

---

<div class="post-metadata">

**Author:** ![Malcolm\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/54ee81/32.png) [@Malcolm\_L](https://www.ruby-forum.com/u/Malcolm_L)\
**Post date:** [November 7, 2007, 8:27am UTC](https://www.ruby-forum.com/t/define-method/119460/3 "2007-11-07T08:27:44Z")

</div>

You are getting this error since define\_method is defined in Module  
not Object. Your object needs to call define\_method against it’s Class  
definition, not self.

Here’s an example how to use it.  
[http://www.ruby-doc.org/core/classes/Module.html#M001677](http://www.ruby-doc.org/core/classes/Module.html#M001677)

Best,  
Jake

---

<div class="post-metadata">

**Author:** ![Malcolm\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/54ee81/32.png) [@Malcolm\_L](https://www.ruby-forum.com/u/Malcolm_L)\
**Post date:** [November 7, 2007, 1:42pm UTC](https://www.ruby-forum.com/t/define-method/119460/4 "2007-11-07T13:42:39Z")

</div>

2007/11/7, Malcolm L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> x = TestClass.new  
> x.hello
> 
> I am always getting:  
> NoMethodError: undefined method `define\_method’
> 
> Do I need to require something? Am I doing something wrong, or am I  
> completely missing the boat!?

Is this really your use case? Since you know which method you define,  
you can do it with a normal def anyway. So I suspect that you are  
doing something else. What exactly is it?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Malcolm\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/54ee81/32.png) [@Malcolm\_L](https://www.ruby-forum.com/u/Malcolm_L)\
**Post date:** [November 8, 2007, 3:26pm UTC](https://www.ruby-forum.com/t/define-method/119460/5 "2007-11-08T15:26:21Z")

</div>

Hey, thanks for the replies guys, I am pretty noob with ruby so thanks  
for your help.

Yes Robert, that wasn’t my use case - that was just the most  
simplified code that shows the problem I was having. I didn’t want to  
bore you all with loads of irrelevant code. If you are interested I  
was trying to build a dynamic enumeration style class (vaguely similar  
to java or c#'s way of doing it), where I could have the pseudo  
constant (i.e. TestClass.Blue, TestClass.Red etc.) setup by calling  
the constructor something like TestClass.new(:Blue =\> 1, :Red =\> 2  
etc.) and also be able to iterate through the keys and stuff.

Anyway, thanks again for your help guys.  
Malcolm

---

<div class="post-metadata">

**Author:** ![Malcolm\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/54ee81/32.png) [@Malcolm\_L](https://www.ruby-forum.com/u/Malcolm_L)\
**Post date:** [November 8, 2007, 4:37pm UTC](https://www.ruby-forum.com/t/define-method/119460/6 "2007-11-08T16:37:29Z")

</div>

Please do not top post.

On 07.11.2007 22:19, Malcolm L. wrote:

> Yes Robert, that wasn’t my use case - that was just the most  
> simplified code that shows the problem I was having. I didn’t want to  
> bore you all with loads of irrelevant code. If you are interested I  
> was trying to build a dynamic enumeration style class (vaguely similar  
> to java or c#'s way of doing it), where I could have the pseudo  
> constant (i.e. TestClass.Blue, TestClass.Red etc.) setup by calling  
> the constructor something like TestClass.new(:Blue =\> 1, :Red =\> 2  
> etc.) and also be able to iterate through the keys and stuff.

In case you are not doing it for the fun of it and / or are looking for  
more inspirations this is a good place to look:

[http://raa.ruby-lang.org/search.rhtml?search=enum](http://raa.ruby-lang.org/search.rhtml?search=enum)

Kind regards

robert
