# Re-define class method

**URL:** <https://rubytalk.org/t/re-define-class-method/28920>\
**Category:** ruby-talk\
**Created:** [13 July 2006 02:58 UTC](https://rubytalk.org/t/re-define-class-method/28920 "2006-07-13T02:58:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Chelimsky](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@David\_Chelimsky](https://rubytalk.org/u/David_Chelimsky)\
**Post date:** [13 July 2006 02:58 UTC](https://rubytalk.org/t/re-define-class-method/28920/1 "2006-07-13T02:58:56Z")

</div>

I've got the following:

module ClassMethods  
&nbsp;&nbsp;&nbsp;def class\_method  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Class method"  
&nbsp;&nbsp;&nbsp;end  
end

class Thing  
&nbsp;&nbsp;&nbsp;extend ClassMethods  
end

Thing.class\_method  
=\> "Class method"

... and I want to change the behaviour of that method at runtime. I tried this:

Thing.send(:define\_method, :class\_method, lambda{ "replaced" })

... but I still get this:

Thing.class\_method  
=\> "Class method"

Am I close? What's the right way to do this?

Thanks,  
David

---

<div class="post-metadata">

**Author:** ![David\_Chelimsky](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@David\_Chelimsky](https://rubytalk.org/u/David_Chelimsky)\
**Post date:** [13 July 2006 03:08 UTC](https://rubytalk.org/t/re-define-class-method/28920/2 "2006-07-13T03:08:42Z")

</div>

OK - I've discovered that I can do this:

Thing.class.send(:define\_method, :other\_method, lambda{ "other" })  
Thing.other\_method  
=\> "other"

and even redefine it:

Thing.class.send(:define\_method, :other\_method, lambda{ "other redefined" })  
Thing.other\_method  
=\> "other redefined"

but if I try this:

Thing.class.send(:define\_method, :class\_method, lambda{ "replaced" })  
Thing.class\_method  
=\> "Class method"

So it looks like I can replace class methods that I define at runtime, but not the ones that were defined at load-time. Is that correct? Am I missing a step?

Thanks,  
David

> **···**
>
> On Jul 12, 2006, at 9:58 PM, David Chelimsky wrote:
> 
> > I've got the following:
> > 
> > module ClassMethods  
> > &nbsp;&nbsp;def class\_method  
> > &nbsp;&nbsp;&nbsp;&nbsp;"Class method"  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class Thing  
> > &nbsp;&nbsp;extend ClassMethods  
> > end
> > 
> > Thing.class\_method  
> > =\> "Class method"
> > 
> > ... and I want to change the behaviour of that method at runtime. I tried this:
> > 
> > Thing.send(:define\_method, :class\_method, lambda{ "replaced" })
> > 
> > ... but I still get this:
> > 
> > Thing.class\_method  
> > =\> "Class method"
> > 
> > Am I close? What's the right way to do this?

---

<div class="post-metadata">

**Author:** ![David\_Chelimsky](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@David\_Chelimsky](https://rubytalk.org/u/David_Chelimsky)\
**Post date:** [13 July 2006 10:24 UTC](https://rubytalk.org/t/re-define-class-method/28920/3 "2006-07-13T10:24:10Z")

</div>

Well, I discovered the solution, thanks to our friend google, and got a much better understanding of singleton classes (though the fog hasn't completely dissipated). For anyone interested:

thing\_singleton\_class = class \<\< Thing; self; end  
thing\_singleton\_class.send(:define\_method, :class\_method, proc { "replaced" })

Thing.class\_method  
=\> "replaced"

Cheers,  
David

> **···**
>
> On Jul 12, 2006, at 10:08 PM, David Chelimsky wrote:
> 
> > On Jul 12, 2006, at 9:58 PM, David Chelimsky wrote:
> > 
> > > I've got the following:
> > > 
> > > module ClassMethods  
> > > &nbsp;&nbsp;def class\_method  
> > > &nbsp;&nbsp;&nbsp;&nbsp;"Class method"  
> > > &nbsp;&nbsp;end  
> > > end
> > > 
> > > class Thing  
> > > &nbsp;&nbsp;extend ClassMethods  
> > > end
> > > 
> > > Thing.class\_method  
> > > =\> "Class method"
> > > 
> > > ... and I want to change the behaviour of that method at runtime. I tried this:
> > > 
> > > Thing.send(:define\_method, :class\_method, lambda{ "replaced" })
> > > 
> > > ... but I still get this:
> > > 
> > > Thing.class\_method  
> > > =\> "Class method"
> > > 
> > > Am I close? What's the right way to do this?
> > 
> > OK - I've discovered that I can do this:
> > 
> > Thing.class.send(:define\_method, :other\_method, lambda{ "other" })  
> > Thing.other\_method  
> > =\> "other"
> > 
> > and even redefine it:
> > 
> > Thing.class.send(:define\_method, :other\_method, lambda{ "other redefined" })  
> > Thing.other\_method  
> > =\> "other redefined"
> > 
> > but if I try this:
> > 
> > Thing.class.send(:define\_method, :class\_method, lambda{ "replaced" })  
> > Thing.class\_method  
> > =\> "Class method"
> > 
> > So it looks like I can replace class methods that I define at runtime, but not the ones that were defined at load-time. Is that correct? Am I missing a step?
> > 
> > Thanks,  
> > David

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [13 July 2006 10:33 UTC](https://rubytalk.org/t/re-define-class-method/28920/4 "2006-07-13T10:33:22Z")

</div>

Hi --

> **···**
>
> On Thu, 13 Jul 2006, David Chelimsky wrote:
> 
> > Well, I discovered the solution, thanks to our friend google, and got a much better understanding of singleton classes (though the fog hasn't completely dissipated). For anyone interested:
> > 
> > thing\_singleton\_class = class \<\< Thing; self; end  
> > thing\_singleton\_class.send(:define\_method, :class\_method, proc { "replaced" })
> > 
> > Thing.class\_method  
> > =\> "replaced"
> 
> Don't forget that you can do this much more concisely (unless you have  
> a specific need to go the long way round):
> 
> &nbsp;&nbsp;&nbsp;def Thing.class\_method  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"replaced"  
> &nbsp;&nbsp;&nbsp;end
> 
> David
> 
> --  
> [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
> [Ruby for Rails](http://www.manning.com/black) =\> RUBY FOR RAILS, the Ruby book for  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rails developers  
> [http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack's][Web]log  
> dblack@wobblini.net =\> me

---

<div class="post-metadata">

**Author:** ![David\_Chelimsky](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@David\_Chelimsky](https://rubytalk.org/u/David_Chelimsky)\
**Post date:** [13 July 2006 11:13 UTC](https://rubytalk.org/t/re-define-class-method/28920/5 "2006-07-13T11:13:11Z")

</div>

Thanks David. I definitely appreciate the conciseness of this, and will use that when I know the type. The problem I'm working on requires a solution to replacing singleton methods on arbitrary types at runtime, so I think I have to stick w/ the more cryptic version. Unless you have a different suggestion - I'm just getting my head wrapped around metaprogramming and singleton classes, so all suggestions are most welcome.

Thanks,  
David

> **···**
>
> On Jul 13, 2006, at 5:33 AM, dblack@wobblini.net wrote:
> 
> > On Thu, 13 Jul 2006, David Chelimsky wrote:
> > 
> > > thing\_singleton\_class = class \<\< Thing; self; end  
> > > thing\_singleton\_class.send(:define\_method, :class\_method, proc { "replaced" })
> > > 
> > > Thing.class\_method  
> > > =\> "replaced"
> > 
> > Don't forget that you can do this much more concisely (unless you have  
> > a specific need to go the long way round):
> > 
> > &nbsp;&nbsp;def Thing.class\_method  
> > &nbsp;&nbsp;&nbsp;&nbsp;"replaced"  
> > &nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [13 July 2006 11:24 UTC](https://rubytalk.org/t/re-define-class-method/28920/6 "2006-07-13T11:24:03Z")

</div>

Hi --

> > > thing\_singleton\_class = class \<\< Thing; self; end  
> > > thing\_singleton\_class.send(:define\_method, :class\_method, proc { "replaced" })
> > > 
> > > Thing.class\_method  
> > > =\> "replaced"
> > 
> > Don't forget that you can do this much more concisely (unless you have  
> > a specific need to go the long way round):
> > 
> > def Thing.class\_method  
> > &nbsp;&nbsp;&nbsp;"replaced"  
> > end
> 
> Thanks David. I definitely appreciate the conciseness of this, and will use that when I know the type. The problem I'm working on requires a solution to replacing singleton methods on arbitrary types at runtime, so I think I have to stick w/ the more cryptic version. Unless you have a different suggestion - I'm just getting my head wrapped around metaprogramming and singleton classes, so all suggestions are most welcome.

There's an intermediate version, which might be usable depending on  
how much you know in advance:

&nbsp;&nbsp;&nbsp;class \<\< Thing  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def class\_method  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"replaced"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

When you need a less concise version, because you're determining  
things more dynamically, I would personally tend to use class\_eval and  
define\_method without send:

meth = "class\_method"  
l = lambda { "replaced" }  
(class \<\< Thing; self; end).class\_eval { define\_method(meth,&l) }

And hopefully we'll eventually have a Kernel#singleton\_class method to  
make it all a little neater 🙂

David

> **···**
>
> On Thu, 13 Jul 2006, David Chelimsky wrote:
> 
> > On Jul 13, 2006, at 5:33 AM, dblack@wobblini.net wrote:
> > 
> > > On Thu, 13 Jul 2006, David Chelimsky wrote:
> 
> --  
> [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com) =\> Ruby/Rails training & consultancy  
> [Ruby for Rails](http://www.manning.com/black) =\> RUBY FOR RAILS (reviewed on  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Slashdot, 7/12/2006!)  
> [http://dablog.rubypal.com](http://dablog.rubypal.com) =\> D[avid]A[.]B[lack's][Web]log  
> dblack@wobblini.net =\> me

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)\
**Post date:** [13 July 2006 12:47 UTC](https://rubytalk.org/t/re-define-class-method/28920/7 "2006-07-13T12:47:37Z")

</div>

You can use David's method like this:

def redefine\_method\_for(klass)  
&nbsp;&nbsp;def klass.class\_method  
&nbsp;&nbsp;&nbsp;&nbsp;"replaced"  
&nbsp;&nbsp;end  
end

redefine\_method\_for Thing

p Thing.class\_method  
#=\> "replaced"

Regards,  
Sean

> **···**
>
> On 7/13/06, David Chelimsky \<david@objectmentor.com\> wrote:
> 
> > On Jul 13, 2006, at 5:33 AM, dblack@wobblini.net wrote:
> > 
> > \> On Thu, 13 Jul 2006, David Chelimsky wrote:  
> > \>  
> > \>\> thing\_singleton\_class = class \<\< Thing; self; end  
> > \>\> thing\_singleton\_class.send(:define\_method, :class\_method, proc  
> > \>\> { "replaced" })  
> > \>\>  
> > \>\> Thing.class\_method  
> > \>\> =\> "replaced"  
> > \>  
> > \> Don't forget that you can do this much more concisely (unless you have  
> > \> a specific need to go the long way round):  
> > \>  
> > \> def Thing.class\_method  
> > \> "replaced"  
> > \> end
> > 
> > Thanks David. I definitely appreciate the conciseness of this, and  
> > will use that when I know the type. The problem I'm working on  
> > requires a solution to replacing singleton methods on arbitrary types  
> > at runtime, so I think I have to stick w/ the more cryptic version.  
> > Unless you have a different suggestion - I'm just getting my head  
> > wrapped around metaprogramming and singleton classes, so all  
> > suggestions are most welcome.
> > 
> > Thanks,  
> > David

---

<div class="post-metadata">

**Author:** ![David\_Chelimsky](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@David\_Chelimsky](https://rubytalk.org/u/David_Chelimsky)\
**Post date:** [13 July 2006 11:59 UTC](https://rubytalk.org/t/re-define-class-method/28920/8 "2006-07-13T11:59:31Z")

</div>

Excellent! Much cleaner.

Thanks,  
David

> **···**
>
> On Jul 13, 2006, at 6:24 AM, dblack@wobblini.net wrote:
> 
> > > > On Thu, 13 Jul 2006, David Chelimsky wrote:
> > > > 
> > > > > thing\_singleton\_class = class \<\< Thing; self; end  
> > > > > thing\_singleton\_class.send(:define\_method, :class\_method, proc { "replaced" })  
> > > > > Thing.class\_method  
> > > > > =\> "replaced"
> > 
> > I would personally tend to use class\_eval and define\_method without send:
> > 
> > meth = "class\_method"  
> > l = lambda { "replaced" }  
> > (class \<\< Thing; self; end).class\_eval { define\_method(meth,&l) }

---

<div class="post-metadata">

**Author:** ![David\_Chelimsky](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@David\_Chelimsky](https://rubytalk.org/u/David_Chelimsky)\
**Post date:** [13 July 2006 14:01 UTC](https://rubytalk.org/t/re-define-class-method/28920/9 "2006-07-13T14:01:04Z")

</div>

Unfortunately (actually, rather fortunately 😉 ), both the class and the method name are determined at run time. David's dynamic suggestion (elsewhere in this thread) is working quite well.

Cheers, and thanks for the help!

David

> **···**
>
> On Jul 13, 2006, at 7:47 AM, Sean O'Halpin wrote:
> 
> > On 7/13/06, David Chelimsky \<david@objectmentor.com\> wrote:
> > 
> > > On Jul 13, 2006, at 5:33 AM, dblack@wobblini.net wrote:  
> > > \> Don't forget that you can do this much more concisely (unless you have  
> > > \> a specific need to go the long way round):  
> > > \>  
> > > \> def Thing.class\_method  
> > > \> "replaced"  
> > > \> end
> > 
> > You can use David's method like this:
> > 
> > def redefine\_method\_for(klass)  
> > def klass.class\_method  
> > &nbsp;&nbsp;&nbsp;"replaced"  
> > end  
> > end
> > 
> > redefine\_method\_for Thing
> > 
> > p Thing.class\_method  
> > #=\> "replaced"
