From: David Chelimsky Date: 2006-07-13T19:24:10+09:00 Subject: Re: re-define class method 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 >> def class_method >> "Class method" >> end >> end >> >> class Thing >> 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 > > >