In message "[ruby-talk:6503] redefining methods in a hierarchy."
on 00/11/22, Hugh Sasse Staff Elec Eng <hgs / dmu.ac.uk> writes:
>If I have an object which I know to be a subclass of a subclass (at lease)
>and I want to override a method, I can do it in that object's class and
>all is well. But if I can override the method higher up I get more power
>from my change. My change may not just affect Fixnums, but it might be
>able to work on all numbers, for example. Given the reflection abilities
>of Ruby, can I ask an object which class really handles a given method?
>Then I know where to make the change.
Are you looking for something like this?
class Class
def whereis(meth)
meth = meth.id2name if meth.is_a? Symbol
unless meth.is_a? String
raise TypeError,
"wrong argument type #{meth.type} (expected String or Symbol)"
end
ancestors.find do |klass|
klass.instance_methods.include? meth
end
end
end
p Fixnum.whereis :nonzero? #=> Numeric
p Fixnum.whereis :integer? #=> Integer
p Fixnum.whereis :zero? #=> Fixnum
p Array.whereis :find #=> Enumerable
-- gotoken