Stefano Crocco
1/10/2008 10:54:00 AM
Alle gioved=EC 10 gennaio 2008, botp ha scritto:
> On Jan 10, 2008 6:12 PM, Stefano Crocco <stefano.crocco@alice.it> 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=20
called. If a superclass defines the same method, the superclass's method wi=
ll=20
be called. According to ri, instead, undef_method, prevents the class from=
=20
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 =3D B.new
b.test
=3D> test for class B
class B
remove_method :test
end
b.test
=3D> test for class A
class B
undef_method :test
end
b.test
=3D> undefined method `test' for #<B:0xb7c9a754> (NoMethodError)
In my previous post, used undef_method instead of remove_method thinking=20
(without trying it) that remove_method would only remove it from the=20
singleton class. I tried it now, and I saw it works with both methods. It=20
seems singleton classes are treated in a different way.
Stefano