# How can we create instance method and class methods at runtime in ruby?

**URL:** <https://rubytalk.org/t/how-can-we-create-instance-method-and-class-methods-at-runtime-in-ruby/68231>\
**Category:** ruby-talk\
**Created:** [23 July 2013 04:43 UTC](https://rubytalk.org/t/how-can-we-create-instance-method-and-class-methods-at-runtime-in-ruby/68231 "2013-07-23T04:43:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [23 July 2013 04:43 UTC](https://rubytalk.org/t/how-can-we-create-instance-method-and-class-methods-at-runtime-in-ruby/68231/1 "2013-07-23T04:43:41Z")

</div>

How can we create instance method and class methods at runtime in ruby?

class Foo  
&nbsp;&nbsp;def test  
&nbsp;&nbsp;&nbsp;&nbsp;p "hi"  
&nbsp;&nbsp;end  
end  
foo = Foo.new  
foo.test # =\> "hi"  
class Foo  
&nbsp;&nbsp;def test  
&nbsp;&nbsp;&nbsp;&nbsp;p "hiyy"  
&nbsp;&nbsp;end  
end  
foo.test  
# \>\> "hi"  
# \>\> "hiyy"

How Can I re-define the `test` method at runtime? I tried  
`define_method`,but it didn't work for this.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Tamara\_Temple1](https://avatars.discourse-cdn.com/v4/letter/t/5f9b8f/32.png) [@Tamara\_Temple1](https://rubytalk.org/u/Tamara_Temple1)\
**Post date:** [23 July 2013 05:23 UTC](https://rubytalk.org/t/how-can-we-create-instance-method-and-class-methods-at-runtime-in-ruby/68231/2 "2013-07-23T05:23:10Z")

</div>

Don't know what you're doing, but this works:

$ irb  
2.0.0p247 :001 \> class Foo; def test; p "hi"; end;end  
=\> nil  
2.0.0p247 :002 \> foo = Foo.new  
=\> #\<Foo:0x007fdef1882f20\>  
2.0.0p247 :003 \> foo.test  
"hi"  
=\> "hi"  
2.0.0p247 :004 \> class Foo; def test; p "bye"; end;end  
=\> nil  
2.0.0p247 :005 \> foo.test  
"bye"  
=\> "bye"  
2.0.0p247 :006 \> class Foo; def self.test; p "bazinga!"; end;end  
=\> nil  
2.0.0p247 :007 \> Foo.test  
"bazinga!"  
=\> "bazinga!"  
2.0.0p247 :008 \> foo.test  
"bye"  
=\> "bye"

> **···**
>
> On Jul 22, 2013, at 11:43 PM, Love U Ruby \<lists@ruby-forum.com\> wrote:
> 
> > How can we create instance method and class methods at runtime in ruby?
> > 
> > class Foo  
> > def test  
> > &nbsp;&nbsp;&nbsp;p "hi"  
> > end  
> > end  
> > foo = Foo.new  
> > foo.test # =\> "hi"  
> > class Foo  
> > def test  
> > &nbsp;&nbsp;&nbsp;p "hiyy"  
> > end  
> > end  
> > foo.test  
> > # \>\> "hi"  
> > # \>\> "hiyy"
> > 
> > How Can I re-define the `test` method at runtime? I tried  
> > `define_method`,but it didn't work for this.
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [23 July 2013 06:51 UTC](https://rubytalk.org/t/how-can-we-create-instance-method-and-class-methods-at-runtime-in-ruby/68231/3 "2013-07-23T06:51:18Z")

</div>

Sorry for creating the confusions:-

Here I tried once more to explain what I was looking for:

class Foo  
&nbsp;&nbsp;def test  
&nbsp;&nbsp;&nbsp;&nbsp;p "hi"  
&nbsp;&nbsp;end  
end  
foo = Foo.new  
foo.test # =\> "hi"  
Foo.define\_method(:test){p "hello"}  
# private method `define\_method' called for Foo:Class (NoMethodError)

Any method is there to re-define the method `test` without re-opening  
the class in runtime?

Thanks

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [23 July 2013 07:01 UTC](https://rubytalk.org/t/how-can-we-create-instance-method-and-class-methods-at-runtime-in-ruby/68231/4 "2013-07-23T07:01:04Z")

</div>

You can use class\_eval:

Foo.class\_eval do  
&nbsp;&nbsp;define\_method(:test){p 'hello'}  
end

or you can use send:

Foo.send :define\_method, :test, &proc{p 'hello'}

I hope this helps

Stefano

> **···**
>
> On Tuesday 23 July 2013 Love U Ruby wrote
> 
> > Sorry for creating the confusions:-
> > 
> > Here I tried once more to explain what I was looking for:
> > 
> > class Foo  
> > &nbsp;&nbsp;def test  
> > &nbsp;&nbsp;&nbsp;&nbsp;p "hi"  
> > &nbsp;&nbsp;end  
> > end  
> > foo = Foo.new  
> > foo.test # =\> "hi"  
> > Foo.define\_method(:test){p "hello"}  
> > # private method `define\_method' called for Foo:Class (NoMethodError)
> > 
> > Any method is there to re-define the method `test` without re-opening  
> > the class in runtime?
> > 
> > Thanks
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)\
**Post date:** [23 July 2013 11:18 UTC](https://rubytalk.org/t/how-can-we-create-instance-method-and-class-methods-at-runtime-in-ruby/68231/5 "2013-07-23T11:18:31Z")

</div>

Stefano Crocco wrote in post #1116318:

> **···**
>
> > On Tuesday 23 July 2013 Love U Ruby wrote
> > 
> > Foo.class\_eval do  
> > &nbsp;&nbsp;define\_method(:test){p 'hello'}  
> > end
> > 
> > or you can use send:
> > 
> > Foo.send :define\_method, :test, &proc{p 'hello'}
> > 
> > I hope this helps
> > 
> > Stefano
> 
> Thanks @Stefano - this is what I was looking for.. 🙂
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Tamara\_Temple1](https://avatars.discourse-cdn.com/v4/letter/t/5f9b8f/32.png) [@Tamara\_Temple1](https://rubytalk.org/u/Tamara_Temple1)\
**Post date:** [23 July 2013 13:29 UTC](https://rubytalk.org/t/how-can-we-create-instance-method-and-class-methods-at-runtime-in-ruby/68231/6 "2013-07-23T13:29:41Z")

</div>

Be circumspect in doing this with define\_method. It's a lot slower when you call the method:

> <https://gist.github.com/tribalvibes/1123584>

> **···**
>
> On Jul 23, 2013, at 6:18 AM, Love U Ruby \<lists@ruby-forum.com\> wrote:
> 
> > Stefano Crocco wrote in post #1116318:
> > 
> > > On Tuesday 23 July 2013 Love U Ruby wrote
> > > 
> > > Foo.class\_eval do  
> > > define\_method(:test){p 'hello'}  
> > > end
> > > 
> > > or you can use send:
> > > 
> > > Foo.send :define\_method, :test, &proc{p 'hello'}
> > > 
> > > I hope this helps
> > > 
> > > Stefano
> > 
> > Thanks @Stefano - this is what I was looking for.. 🙂
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
