-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TreeNode#find #22
Comments
The This article describes the rationale behind As you can see from the method documentation for find, the intent of this method is to search the whole collection, and in the specific case for RubyTree, will definitely use the same semantics for the collection as Perhaps an example will help: require "rubytree"
if __FILE__ == $0
root_node = Tree::TreeNode.new("ROOT", "Root Content")
root_node << Tree::TreeNode.new("CHILD1", "Child1 Content") << Tree::TreeNode.new("GRANDCHILD1", "GrandChild1 Content")
root_node << Tree::TreeNode.new("CHILD2", "Child2 Content")
puts root_node.find {|node| node.name == "ROOT"} || "Not Found" # Find the current node itself
puts root_node.find {|node| node.name == "CHILD1"} || "Not Found" # Find another node in the collection
end In this example, both the current node as well as another arbitrary node within the tree collection are found using |
Again, Anupam, thanks for your usual comprehensive response!
On December 19, 2013 at 10:53:53 PM, Anupam Sengupta (notifications@github.com) wrote: The each method is actually required in order for Enumerable to work. See details in the Enumerable module documentation. This article describes the rationale behind Enumerable, and how to use it, in more detail. As you can see from the method documentation for find, the intent of this method is to search the whole collection, and in the specific case for RubyTree, will definitely use the same semantics for the collection as TreeNode#each, i.e., will start searching from the node itself. Perhaps an example will help: require "rubytree" if FILE == $0 root_node << Tree::TreeNode.new("CHILD1", "Child1 Content") << Tree::TreeNode.new("GRANDCHILD1", "GrandChild1 Content") puts root_node.find {|node| node.name == "ROOT"} || "Not Found" # Find the current node itself — |
I understand that TreeNode#each walks the whole subtree starting at itself.
I would have thought that TreeNode#find would search similarly as it also comes from Enumerable, but I must be misunderstanding. Can you explain?
The text was updated successfully, but these errors were encountered: