Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For instance methods there is an <code>Object::method_added(name)</code> method you can override, similar to 'inherited' you have used:</p> <pre><code>class test def self.method_added(name) puts "method_added(#{name.inspect})" super end end irb(main):002:0&gt; class Child &lt; Test; def foo; end; end method_added(:foo) =&gt; nil </code></pre> <p>You can then compare a received name to a list of your methods:</p> <pre><code>Test.instance_methods.include?(name.to_s) </code></pre> <p>With class methods this approach does not work (even if you do things like <code>class &lt;&lt; self</code> magic), but a helpful fellow knew the answer: <a href="http://www.ruby-forum.com/topic/120416" rel="nofollow">http://www.ruby-forum.com/topic/120416</a> :</p> <pre><code>class Test def self.singleton_method_added(name) puts "Class method added #{name.inspect}" end end </code></pre> <p>This is only the first part of the problem, because you need to know which class defined the method (it will be self) and whether the method is a new one, or overridden one. Experiment with this code:</p> <pre><code>class Test def self.singleton_method_added(name) if self == Test puts "My own class method added: #{self.name}.#{name.inspect}" elsif Test.methods(false).include?(name.to_s) puts "Class method overriden: #{self.name}.#{name.inspect}" elsif Test.methods(true).include?(name.to_s) puts "My parent's class method overriden: #{self.name}.#{name.inspect}" else puts "New class method added: #{self.name}.#{name.inspect}" end end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload