Summit Guy
Summit Guy

Reputation: 305

How do I traverse all the nodes in a YAML tree in Ruby?

I am loading an arbitrary YAML document, and want to walk every node in the tree. I don't know how nested the tree is beforehand, so I can't just use a simple each statement to walk all the nodes.

Here is how I'm loading the document:

tree = File.open( "#{RAILS_ROOT}/config/locales/es.yml" ){ |yf| YAML::load (yf)}

Upvotes: 3

Views: 4998

Answers (2)

1gor
1gor

Reputation: 126

Here is how to parse a YAML tree structure and to act on each node, branch and leaf. It also gives you parent of each node that you need, for example, when populating a database tree structure from your YAML file. It is a small addition to @sepp2k excellent answer:

require 'yaml'

def traverse(obj,parent, &blk)
 case obj
 when Hash
   obj.each do |k,v| 
     blk.call(k,parent)
     # Pass hash key as parent
     traverse(v,k, &blk) 
   end
 when Array
   obj.each {|v| traverse(v, parent, &blk) }
 else
   blk.call(obj,parent)
 end
end


# Example, creating a database tree structure, from your YAML file.
# Passing nil to root node(s) as parent

tree_str =<<EOF
  Regions:
    - Asia
    - Europe
    - Americas
  Other:
    - Foo:
      - bar
      - buz
EOF

traverse( YAML.load(tree_str), nil ) do |node,parent|
  puts "Creating node '#{node}' with parent '#{ parent || 'nil' }'"
end

Upvotes: 5

sepp2k
sepp2k

Reputation: 370425

def traverse(obj, &blk)
  case obj
  when Hash
    # Forget keys because I don't know what to do with them
    obj.each {|k,v| traverse(v, &blk) }
  when Array
    obj.each {|v| traverse(v, &blk) }
  else
    blk.call(obj)
  end
end

traverse( YAML.load_file(filename) ) do |node|
  puts node
end

Edit:

Note that this only yields the leaf nodes. The question wasn't very clear as to what was wanted exactly.

Upvotes: 14

Related Questions