On Jan 21, 8:34 am, RHS <RHSee... / gmail.com> wrote: > Out of curiosity... operator overloading and it's ilk are (by many) > considered to be bad things because it can make it hard to figure out > what's happening in the code, especially for dynamic languages where > they can change on the fly. The reason for this thought is, I believe, > because it can be difficult to understand what code is doing if the > definitions of "standard" operations are or where they come from. > > Along those lines, is there a current way to ask Ruby where the > definition comes from for a given operator or, more generally, method? > Ie, Ruby needs to do a lookup to find out what definition to use. Is > there a way to ask it to do that lookup and report the results? > > Thanks, > Rob Seeger In fact you can, Rob. It's the 'method' method. Here's an IRB dump with some explanations >> [].method(:[]) => #<Method: Array#[]> # [] (an Array) gets the method [] from Array >> class Blah; end => nil >> Blah.new.method(:==) => #<Method: Blah(Kernel)#==> # Blah.new (an instance of class Blah) has the == method from the Kernel module (Object is the base class of all Objects and it has Kernel mixed in) >> Blah.new.method(:<=) NameError: undefined method `<=' for class `Blah' from (irb):5:in `method' from (irb):5 # Blah.new (an instance of class Blah) has no <= method >> class Blah; include Comparable; end => Blah >> Blah.new.method(:==) => #<Method: Blah(Comparable)#==> # mixing in the Comparable module gives some of its own definitions for a few methods, like ==. That's where Blah's == comes from now, not Kernel. >> Blah.new.method(:<=) => #<Method: Blah(Comparable)#<=> # and now Blah also has a <= method thanks to Comparable >> class Blah; def <=(other); end; end => nil >> Blah.new.method(:<=) => #<Method: Blah#<=> # and now Blah has it's own <= method defined That'll tell you *where* the method comes from. What you do with it is up to you. -- -yossef