Skip to content

Instantly share code, notes, and snippets.

/ruby.rb Secret

Created August 15, 2016 13:02
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/63446b11880df7ac961f8a747d3dba94 to your computer and use it in GitHub Desktop.
Save anonymous/63446b11880df7ac961f8a747d3dba94 to your computer and use it in GitHub Desktop.
class Tree
attr_accessor :children, :node_name
def initialize(other={})
if not other.nil?
key, children = other.first
@node_name = key;
@children = []
other.values.each {|x| @children << Tree.new(x)}
end
end
def visit_all(&block)
visit &block
children.each {|c| c.visit_all &block} unless children.nil?
end
def visit(&block)
block.call self
end
end
t = Tree.new({'grandpa'=>{'dad'=>{'child1'=>{},'child2'=>{}},'uncle'=>{'child3'=>{},'child4'=>{}}}})
t.visit_all {|x| puts x.node_name}
gets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment