title: How to evaluate methods of another class in current context?
body: <p>I have 2 classes whose object should act as "partners". The first one is my <code>Thing</code> class, whose instances should act as <code>Tree::TreeNode</code>s of the gem RubyTree.</p>
  
  <p>Basically, this delegation can be implemented using <code>Forwardable</code>:</p>
  
  <pre><code>class Thing &lt; NoClassInheritancePlease
    extend Forwardable
  
    def initialize(title = "node")
      @node = Tree::TreeNode.new title
  
      # Collect node methods that should be delegated
      node_methods = @node.public_methods(false)
      node_methods += @node.protected_methods
      node_methods -= (public_methods(false) + protected_methods(false) + private_methods) # own methods should not been delegated
  
      # Set up delegation of specified node methods as singleton methods
      for method in node_methods
        Base.def_delegator :@node, method
      end
    end
  end
  </code></pre>
  
  <p><strong>Problem:</strong>
  A number of <code>TreeNode</code> methods refer to <code>self</code>. For example:</p>
  
  <pre><code>def each(&amp;block)             # :yields: node
    yield self
    children { |child| child.each(&amp;block) }
  end
  </code></pre>
  
  <p>Thus, <code>my_thing.each {...}</code> yields <code>self</code>, i.e. the <code>Tree::TreeNode</code> object that belongs to <code>my_thing</code> but not the <code>Thing</code> object itself. </p>
  
  <p>Another example:</p>
  
  <pre><code>siblings = []
  parent.children {|my_sibling| siblings &lt;&lt; my_sibling if my_sibling != self}
  siblings
  </code></pre>
  
  <p>parent.children returns an Array of <code>Thing</code>s and therefore the condition never evaluates to false as <code>my_sibling</code> is a <code>Thing</code> (which is fine) but <code>self</code> is a <code>Tree::TreeNode</code></p>
  
  <p><strong>Question</strong>: <em>How to evaluate the instance methods of a class (e.g. <code>Tree::TreeNode</code>) in the context of another class (e.g. <code>Thing</code>)? ("overwrite self")</em></p>
  
  <p>I tried with UnboundMethods, but you can only bind an instance of the original receiving class to an unbound method.</p>