Created
September 19, 2011 15:30
-
-
Save hanachin/1226757 to your computer and use it in GitHub Desktop.
7つの言語7つの世界 p24 Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Tree | |
attr_accessor :children, :node_name | |
def initialize(name, children={}) | |
@children = children | |
@node_name = name | |
end | |
def visit_all(&block) | |
visit &block | |
children.each {|c| c.visit_all &block} | |
end | |
def visit(&block) | |
block.call self | |
end | |
end | |
ruby_tree = Tree.new("Ruby", [Tree.new("Reia"), Tree.new("MacRuby")]) | |
puts "Visiting a node" | |
ruby_tree.visit {|node| puts node.node_name} | |
puts | |
puts "visiting entire tree" | |
ruby_tree.visit_all {|node| puts node.node_name} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment