evolve75/RubyTree

undefined method `replace_with'

Kappie opened this issue · 3 comments

tree1 = Tree::TreeNode.new("tree 1")
tree2 = Tree::TreeNode.new("tree 2")

# NoMethodError: undefined method `replace_with'
tree1.replace_with(tree2)

Also, replace! doesn't exist. Both methods are in the docs. http://rubytree.anupamsg.me/rdoc/Tree/TreeNode.html#replace_with-instance_method

What version of the gem are you using? I don't think there has been a full release since my PR for those methods got merged in.

This works for me when pulling from master

require 'tree'
parent = Tree::TreeNode.new("tree 1")
original_child = Tree::TreeNode.new("Original Child")
new_child = Tree::TreeNode.new("New Child")
parent << original_child
parent.print_tree
original_child.replace_with new_child
parent.print_tree
bundle exec ruby test.rb
* tree 1
+---> Original Child
* tree 1
+---> New Child

There is a problem when you to swap out root nodes, the replace code was written to swap out child nodes as it involves calls to the parent. I should probably also ensure I handle the edge case of swapping out the root node.

I'm using 0.9.4.

I need to swap leaves and subtrees and I'm writing convenience methods for that. I'll send a pull request when I'm done, if you want.

Thanks @Kappie,

If you can create a pull request based on the latest master, then @packetmonkey and I can review and merge in your changes.