undef_method cutting off subclass's method lookup?

T

trans. (T. Onoma)

class Numeric
def succ
self + 1
end
end

class Integer
undef_method( :succ )
end

2.class #=> Fixnum
2.succ
NoMethodError: undefined method `succ' for 1:Fixnum
from (irb):10


Shouldn't it be able to find #succ in the ancestor?

T.
 
D

David A. Black

Hi --

class Numeric
def succ
self + 1
end
end

class Integer
undef_method( :succ )
end

2.class #=> Fixnum
2.succ
NoMethodError: undefined method `succ' for 1:Fixnum
from (irb):10


Shouldn't it be able to find #succ in the ancestor?

From 'ri undef_method':

----------------------------------------------------
Module#undef_method
undef_method(symbol) => self
------------------------------------------------------------------------
Prevents the current class from responding to calls to the named
method. Contrast this with +remove_method+, which deletes the
method from the particular class; Ruby will still search
superclasses and mixed-in modules for a possible receiver.


David
 
T

trans. (T. Onoma)

On Sunday 31 October 2004 03:02 pm, David A. Black wrote:
| Hi --
|
| On Mon, 1 Nov 2004, trans. (T. Onoma) wrote:
| > class Numeric
| > def succ
| > self + 1
| > end
| > end
| >
| > class Integer
| > undef_method( :succ )
| > end
| >
| > 2.class #=> Fixnum
| > 2.succ
| > NoMethodError: undefined method `succ' for 1:Fixnum
| > from (irb):10
| >
| >
| > Shouldn't it be able to find #succ in the ancestor?
|
| From 'ri undef_method':
|
| ----------------------------------------------------
| Module#undef_method
| undef_method(symbol) => self
| ------------------------------------------------------------------------
| Prevents the current class from responding to calls to the named
| method. Contrast this with +remove_method+, which deletes the
| method from the particular class; Ruby will still search
| superclasses and mixed-in modules for a possible receiver.

Sigh. So I should use #remove_method then. Okay. That's bit me again. Yes, I
know this. But I use it rarely, and when I do I keep getting confused about
which is which. The above is terribly written. The statement after the
semicolon doesn't have a clear reference. Is the semicolon supposed to clue
me in? That's weak, and one wonders which of the two will "still search".
Moreover, it says _current class_, not _current class and all subclasses
thereof_, so that seems misleading too.

Anyway, thanks David!
T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top