# Yaml ?nodes? or nested maps

**URL:** <https://www.ruby-forum.com/t/yaml-nodes-or-nested-maps/177129>\
**Category:** Rails\
**Created:** [October 12, 2009, 8:07am UTC](https://www.ruby-forum.com/t/yaml-nodes-or-nested-maps/177129 "2009-10-12T08:07:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [October 12, 2009, 8:07am UTC](https://www.ruby-forum.com/t/yaml-nodes-or-nested-maps/177129/1 "2009-10-12T08:07:50Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [October 12, 2009, 4:37pm UTC](https://www.ruby-forum.com/t/yaml-nodes-or-nested-maps/177129/2 "2009-10-12T16:37:44Z")

</div>

On Oct 12, 2:03 am, Thufir [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [October 13, 2009, 6:40am UTC](https://www.ruby-forum.com/t/yaml-nodes-or-nested-maps/177129/3 "2009-10-13T06:40:48Z")

</div>

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
