62.4. Method ancestors for class Method — reflection

condition="author": NEPUBLIKOVAT, RCR

Opsáno/vypsáno z článku na .

I have some structures that contain objects that refer to those structures. I wanted to write an inspect method. This RCR would allow me to prevent recursion. If Method#== was true when the method is the same method of the same instance, and Method#eql? was true when it was the same method of an instance of the same class (i.e. broader) I could do this to prevent mutual recursion when doing inspect:

class Array
   def inspect
      history = Method.ancestors
      me = history.shift
      if history.detect {|x| x == me)
         # This is a self reference
         result = "Array #{self.id}n"
      else
         # We're examining some new
         # object
         result = "Array #{self.id} [n"
         self.each { |element|
             result += "   #{element.inspect"}n"
         }
         result += "]n"               
      end
      return result                   
   end
end

for example. This would also help in [ruby-talk:52142] I expect. The reason that caller doesn't satisfy my needs here is that it does not tell you which instance it is referring to, and using its information involves manipulating strings. I believe that this suggestion fits in with the philosophy of "eveything being an object".

Licence Creative Commons
Tento dokument Ruby, jehož autorem je Radek Hnilica, podléhá licenci Creative Commons Uveďte autora-Nevyužívejte dílo komerčně-Zachovejte licenci 3.0 Česká republika .