# Basic YAML help - collection of objects with attributes

**URL:** <https://www.ruby-forum.com/t/basic-yaml-help-collection-of-objects-with-attributes/86897>\
**Category:** Ruby\
**Created:** [March 24, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/basic-yaml-help-collection-of-objects-with-attributes/86897 "2006-03-24T22:20:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [March 24, 2006, 10:20pm UTC](https://www.ruby-forum.com/t/basic-yaml-help-collection-of-objects-with-attributes/86897/1 "2006-03-24T22:20:33Z")

</div>

Hi everyone,

I’m trying to output a YAML file that will list users and some of  
their LDAP attributes. For instance (non-YAML):

uid: seanhussey  
dn: uid=seanhussey,dc=company,dc=com  
anotherattr: 39392:12341

The last attr is formatted like that, with the colon, in case that’s  
important.

What I’d like to do is write this out to a file and then be able to  
read it back in such that I can iterate over every user and work on  
the data. My first pass at this gave me this format:

seanhussey:  
dn: uid=seanhussey,dc=company,dc=com  
anotherattr: 39392:12341

Etc, for every user. Although I write a hash to the file, when it all  
comes out, it’s a bunch of arrays. Trying to access pieces of data by  
their name doesn’t work.

users = YAML.load\_file(“users.yaml”)

users.each { |user|  
puts user[0]  
user[1].each { |a|  
puts a[0]  
puts a[1]  
}  
}

This gets me:

seanhussey  
anotherattr  
39392:12341  
dn  
uid=seanhussey,dc=company,dc=com

How do I get to user[:uid], user[:anotherattr] and user[:dn] ?

Thank you!

Sean

---

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [March 25, 2006, 4:04pm UTC](https://www.ruby-forum.com/t/basic-yaml-help-collection-of-objects-with-attributes/86897/2 "2006-03-25T16:04:16Z")

</div>

Sean H. wrote:

> seanhussey:  
> dn: uid=seanhussey,dc=company,dc=com  
> anotherattr: 39392:12341
> 
> Etc, for every user. Although I write a hash to the file, when it all  
> comes out, it’s a bunch of arrays. Trying to access pieces of data by  
> their name doesn’t work.  
> …  
> How do I get to user[:uid], user[:anotherattr] and user[:dn] ?

Why not use the YAML library to produce the file as well as consume it?

require ‘yaml’  
require ‘win32ole’  
domain = WIN32OLE.connect(“WinNT://mydomain”)  
users = {}  
domain.each do |obj|  
if obj.Class == “User”  
users[obj.Name] = {  
:ads\_path =\> obj.AdsPath,  
:guid =\> obj.Guid  
}  
end  
end  
y users

## Output:

Guest:  
:guid: “{D83F1060-1E71-11CF-B1F3-02608C9E7553}”  
:ads\_path: WinNT://MSHOME/TELPERION/Guest  
Administrator:  
:guid: “{D83F1060-1E71-11CF-B1F3-02608C9E7553}”  
:ads\_path: WinNT://MSHOME/TELPERION/Administrator  
Dave:  
:guid: “{D83F1060-1E71-11CF-B1F3-02608C9E7553}”  
:ads\_path: WinNT://MSHOME/TELPERION/Dave  
…

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [March 27, 2006, 4:35pm UTC](https://www.ruby-forum.com/t/basic-yaml-help-collection-of-objects-with-attributes/86897/3 "2006-03-27T16:35:38Z")

</div>

Hmm, nice, I wasn’t away of “y”, so that’s cool.

Writing isn’t where I’m having the issue, though. Once you get that  
file, how do you read it in such that (using your example) you can get  
“Guest” and the appropriate guid and ads\_path?

Thanks!

Sean

---

<div class="post-metadata">

**Author:** ![seanhussey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e57/32.png) [@seanhussey](https://www.ruby-forum.com/u/seanhussey)\
**Post date:** [March 27, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/basic-yaml-help-collection-of-objects-with-attributes/86897/4 "2006-03-27T17:06:29Z")

</div>

Nevermind, I got it. I just had to shift some perceptions.

YAML.load\_file returns an array of arrays, which include a hash for  
the sub-attributes of each object.

Got it.

Thanks!

Sean
