Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Tree
- attr_accessor :children, :node_name
- def initialize(hash)
- @children = []
- hash.keys.each do |key|
- @node_name = key
- hash[key].keys.each do |keychild|
- @children << Tree.new(hash[key][keychild])
- end
- end
- end
- def visit_all(&block)
- visit &block
- children.each {|c| c.visit_all &block}
- end
- def visit(&block)
- block.call self
- end
- def to_s
- puts "Node name: " + @node_name + "\n"
- puts "Children:\n"
- children.each do |c|
- puts "\t" + c.to_s
- end
- end
- end
- x = {"grandpa" => { "dad" => {"child 1" => {}, "child 2" => {} }, "uncle" => {"child 3" => {}, "child 4" => {} } } }
- y = Tree.new(x)
- puts y.to_s
- ./grep.rb:27:in `+': can't convert Array into String (TypeError)
- from ./grep.rb:27:in `to_s'
- from ./grep.rb:26:in `each'
- from ./grep.rb:26:in `to_s'
- from ./grep.rb:34
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement