Skip to content
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

Closed
pitosalas opened this issue Dec 18, 2013 · 2 comments
Closed

TreeNode#find #22

pitosalas opened this issue Dec 18, 2013 · 2 comments

Comments

@pitosalas
Copy link

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?

@evolve75
Copy link
Owner

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("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 TreeNode#find.

@pitosalas
Copy link
Author

Again, Anupam, thanks for your usual comprehensive response!

  • Pito Salas

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("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 TreeNode#find.


Reply to this email directly or view it on GitHub.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants