# Define a class method

**URL:** <https://rubytalk.org/t/define-a-class-method/13407>\
**Category:** ruby-talk\
**Created:** [3 September 2004 16:25 UTC](https://rubytalk.org/t/define-a-class-method/13407 "2004-09-03T16:25:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://rubytalk.org/u/Benny)\
**Post date:** [3 September 2004 16:25 UTC](https://rubytalk.org/t/define-a-class-method/13407/1 "2004-09-03T16:25:17Z")

</div>

dear list,

I know, I can use send( :define\_method, "testmeth") to  
define an object method (i.e. a singleton method of a class).

but how can I define a "normal" method within the class (that is available  
for the objects)

I simply want to define method to which I pass the name and the code for a  
new method to created with a certain class

any ideas?

regards benny

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [3 September 2004 16:50 UTC](https://rubytalk.org/t/define-a-class-method/13407/2 "2004-09-03T16:50:17Z")

</div>

"Benny" \<linux@marcrenearns.de\> schrieb im Newsbeitrag  
news:2prk1uFo78jvU1@uni-berlin.de...

> dear list,
> 
> I know, I can use send( :define\_method, "testmeth") to  
> define an object method (i.e. a singleton method of a class).
> 
> but how can I define a "normal" method within the class (that is available  
> for the objects)
> 
> I simply want to define method to which I pass the name and the code for a  
> new method to created with a certain class
> 
> any ideas?
> 
> regards benny

> > class Foo;end

=\> nil

> > Foo.define\_method( :bar ) { "bar" }

NoMethodError: private method `define\_method' called for Foo:Class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):2

> > Foo.instance\_eval { define\_method( :bar ) { "bar" } }

=\> #\<Proc:0x10180da0@(irb):3\>

> > Foo.new.bar

=\> "bar"

> >

Cheers

&nbsp;&nbsp;&nbsp;&nbsp;robert
