From: Stefano Crocco Date: 2008-01-10T19:54:17+09:00 Subject: Re: how to remove a method from an object ? Alle giovedì 10 gennaio 2008, botp ha scritto: > On Jan 10, 2008 6:12 PM, Stefano Crocco wrote: > > class << s1 > > undef_method :print > > end > > ah, undef_, > > how does it compare then if i do, > class << s1 > remove_method :print > end > > i mean will undef_ do a remove or what? > > but anyway, that is cool Stefano. thanks for the new i've just learned. > > kind regards -botp Well, in general remove_method will remove it only from the class where it's called. If a superclass defines the same method, the superclass's method will be called. According to ri, instead, undef_method, prevents the class from responding to the method at all. Here's an example showing the difference class A def test puts "test for class A" end end class B < A def test puts "test for class B" end end b = B.new b.test => test for class B class B remove_method :test end b.test => test for class A class B undef_method :test end b.test => undefined method `test' for # (NoMethodError) In my previous post, used undef_method instead of remove_method thinking (without trying it) that remove_method would only remove it from the singleton class. I tried it now, and I saw it works with both methods. It seems singleton classes are treated in a different way. Stefano