Yaml ?nodes? or nested maps

I want to iterate ?nodes? and ?leafs? for a yaml document:

thufir@ARRAKIS:~/projects/rss$
thufir@ARRAKIS:~/projects/rss$ ruby user.rb
user.rb:6: undefined method []' for nil:NilClass (NoMethodError) from user.rb:5:in each_key’
from user.rb:5
thufir@ARRAKIS:~/projects/rss$
thufir@ARRAKIS:~/projects/rss$ ruby user2.rb
user2.rb:5: undefined method `[]’ for nil:NilClass (NoMethodError)
thufir@ARRAKIS:~/projects/rss$
thufir@ARRAKIS:~/projects/rss$ nl user.rb
1 require ‘yaml’

 2  yml = YAML::load(File.open('user.yml'))

 3  yml.each_key { |key|
 4    username = yml[key]['user']
 5    password = yml[key]['pass']

 6    puts "#{username} => #{password}"
 7  }

thufir@ARRAKIS:~/projects/rss$
thufir@ARRAKIS:~/projects/rss$ nl user2.rb
1 require ‘yaml’

 2  yml = YAML::load(File.open('user.yml'))

 3  username = yml['juixe']['user']

 4  puts username

thufir@ARRAKIS:~/projects/rss$
thufir@ARRAKIS:~/projects/rss$ nl user.yml
1 juixe:
2 user: juixe-user
3 pass: juixe-pass

 4  techknow:
 5  user: techknow-user
 6  pass: techknow-pass

thufir@ARRAKIS:~/projects/rss$
thufir@ARRAKIS:~/projects/rss$

http://yaml.kwiki.org/index.cgi?YamlInFiveMinutesMinuteThree refers to
this structure as a nested map, FWIW. In either event, how would you
either iterate through the entire structure, or specify the “juixe” map
(or node)?

thanks,

Thufir

On Oct 12, 2:03 am, Thufir [email protected] wrote:

I want to iterate ?nodes? and ?leafs? for a yaml document:
thufir@ARRAKIS:~/projects/rss$ nl user.yml
1 juixe:
2 user: juixe-user
3 pass: juixe-pass

 4  techknow:
 5  user: techknow-user
 6  pass: techknow-pass

Not sure if Groups has eaten the formatting or not - are lines 2, 3, 5
and 6 indented? The YAML won’t work otherwise…

script/console (or irb if you’re not in Rails) is helpful here; try
doing a YAML.load_file(filename) there and see what you get back.

–Matt J.

On Mon, 12 Oct 2009 07:36:19 -0700, Matt J. wrote:

Not sure if Groups has eaten the formatting or not - are lines 2, 3, 5
and 6 indented? The YAML won’t work otherwise…

I use Gmane for this list, but, yeah, things somehow get mangled. No,
they weren’t indented. Works now:

thufir@ARRAKIS:~/projects/rss$
thufir@ARRAKIS:~/projects/rss$
thufir@ARRAKIS:~/projects/rss$ ruby user.rb
techknow-user => techknow-pass
juixe-user => juixe-pass
thufir@ARRAKIS:~/projects/rss$
thufir@ARRAKIS:~/projects/rss$ nl user.yml
1 juixe:
2 user: juixe-user
3 pass: juixe-pass

 4  techknow:
 5    user: techknow-user
 6    pass: techknow-pass

thufir@ARRAKIS:~/projects/rss$

thanks,

Thufir