I have 2 classes whose object should act as "partners". The first one is my Thing class, whose instances should act as Tree::TreeNodes of the gem RubyTree.
Basically, this delegation can be implemented using Forwardable:
class Thing < 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
Problem:
A number of TreeNode methods refer to self. For example:
def each(&block) # :yields: node
yield self
children { |child| child.each(&block) }
end
Thus, my_thing.each {...} yields self, i.e. the Tree::TreeNode object that belongs to my_thing but not the Thing object itself.
Another example:
siblings = []
parent.children {|my_sibling| siblings << my_sibling if my_sibling != self}
siblings
parent.children returns an Array of Things and therefore the condition never evaluates to false as my_sibling is a Thing (which is fine) but self is a Tree::TreeNode
Question: How to evaluate the instance methods of a class (e.g. Tree::TreeNode) in the context of another class (e.g. Thing)? ("overwrite self")
I tried with UnboundMethods, but you can only bind an instance of the original receiving class to an unbound method.