Skip to content

Instantly share code, notes, and snippets.

/ruby.rb Secret

Created May 21, 2016 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/62781fd71f98799a86b1a1dcb271520b to your computer and use it in GitHub Desktop.
Save anonymous/62781fd71f98799a86b1a1dcb271520b to your computer and use it in GitHub Desktop.
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