Mercurial > repos > other > SevenLanguagesInSevenWeeks
annotate 1-Ruby/tree.rb @ 22:e020410896ca
Add rewritten tree that takes a nested hash
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 07 Jan 2017 21:04:20 +0000 |
parents | 7e212cc7ec24 |
children |
rev | line source |
---|---|
21
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 #--- |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 # Excerpted from "Seven Languages in Seven Weeks", |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 # published by The Pragmatic Bookshelf. |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 # Copyrights apply to this code. It may not be used to create training material, |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 # courses, books, articles, and the like. Contact us if you are in doubt. |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 # We make no guarantees that this code is fit for any purpose. |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 # Visit http://www.pragmaticprogrammer.com/titles/btlang for more book information. |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 #--- |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 class Tree |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 attr_accessor :children, :node_name |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 |
22
e020410896ca
Add rewritten tree that takes a nested hash
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
12 def initialize(hash) |
e020410896ca
Add rewritten tree that takes a nested hash
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
13 if hash.size == 1 |
e020410896ca
Add rewritten tree that takes a nested hash
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
14 @node_name = hash.keys[0] |
e020410896ca
Add rewritten tree that takes a nested hash
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
15 @children = hash[@node_name].collect { |key,value| Tree.new({ key => value }) } |
e020410896ca
Add rewritten tree that takes a nested hash
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
16 else |
e020410896ca
Add rewritten tree that takes a nested hash
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
17 @node_name = 'Root' |
e020410896ca
Add rewritten tree that takes a nested hash
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
18 @children = hash.collect { |key,value| Tree.new({ key => value }) } |
e020410896ca
Add rewritten tree that takes a nested hash
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
19 end |
21
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
20 end |
22
e020410896ca
Add rewritten tree that takes a nested hash
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
21 |
21
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 def visit_all(&block) |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
23 visit &block |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
24 children.each {|c| c.visit_all &block} |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
25 end |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
26 |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 def visit(&block) |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
28 block.call self |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
29 end |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
30 end |
7e212cc7ec24
Add default Tree code from book's source code
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
31 |
22
e020410896ca
Add rewritten tree that takes a nested hash
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
32 puts "Creating tree from hashes" |
e020410896ca
Add rewritten tree that takes a nested hash
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
33 hash_tree = Tree.new({'grandpa' => {'dad' => {'child1' => {}, 'child2' => {} }, 'uncle' => { 'child3' => {}, 'child4' => {} } } }) |
e020410896ca
Add rewritten tree that takes a nested hash
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
34 hash_tree.visit_all {|node| puts node.node_name} |
e020410896ca
Add rewritten tree that takes a nested hash
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
35 puts "Testing non-tree hashes" |
e020410896ca
Add rewritten tree that takes a nested hash
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
36 hash_tree = Tree.new({'grandpa' => {'dad' => {'child1' => {}, 'child2' => {} }, 'uncle' => { 'child3' => {}, 'child4' => {} } }, 'other-person' => {'other-child' => {} } }) |
e020410896ca
Add rewritten tree that takes a nested hash
IBBoard <dev@ibboard.co.uk>
parents:
21
diff
changeset
|
37 hash_tree.visit_all {|node| puts node.node_name} |