Yaml ?nodes? or nested maps

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

[email protected]:~/projects/rss$
[email protected]:~/projects/rss$ ruby user.rb
user.rb:6: undefined method []' for nil:NilClass (NoMethodError) from user.rb:5:ineach_key’
from user.rb:5
[email protected]:~/projects/rss$
[email protected]:~/projects/rss$ ruby user2.rb
user2.rb:5: undefined method `[]’ for nil:NilClass (NoMethodError)
[email protected]:~/projects/rss$
[email protected]:~/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  }

[email protected]:~/projects/rss$
[email protected]:~/projects/rss$ nl user2.rb
1 require ‘yaml’

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

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

 4  puts username

[email protected]:~/projects/rss$
[email protected]:~/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

[email protected]:~/projects/rss$
[email protected]:~/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:
[email protected]:~/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:

[email protected]:~/projects/rss$
[email protected]:~/projects/rss$
[email protected]:~/projects/rss$ ruby user.rb
techknow-user => techknow-pass
juixe-user => juixe-pass
[email protected]:~/projects/rss$
[email protected]:~/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

[email protected]:~/projects/rss$

thanks,

Thufir

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs