From: nobu.nokada@... Date: 2002-12-19T03:50:23+09:00 Subject: Re: Mommy, where do methods come from? Hi, At Thu, 19 Dec 2002 03:05:20 +0900, Chris Pine wrote: > Is there a way to tell in which module an object's method was defined? > > For example: > > class Foo > def to_s; 'foo'; end > end > > class Bar; end > > foo = Foo.new > bar = Bar.new module Kernel def methodClass(m) m = "#{m}" self.class.ancestors.find {|c| c.instance_methods.include?(m)} end end > foo.methodClass :to_s # Hopefully returns Foo. > bar.methodClass :to_s # Hopefully returns Object. But bar.to_s is defined in Kernel. Once method of Method returns original class was proposed, but wasn't accepted because of its name. > Furthermore, is there a way to tell all of the modules which did define a > method for a given object? > > foo.methodClasses :to_s # Hopefully returns [Foo, Object]. Replace find with find_all in above. > Finally (and I'm sure I should know this one already), how would I call > Object#to_s on foo if I have already redefined to_s? (Is this even > possible?) Impossible outside redefined to_s, unless it's aliased. -- Nobu Nakada