Alle giovedì 10 gennaio 2008, botp ha scritto:
> 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 #<B:0xb7c9a754> (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
···
On Jan 10, 2008 6:12 PM, Stefano Crocco <stefano.crocco@alice.it> wrote: