# YAML troubles

**URL:** <https://www.ruby-forum.com/t/yaml-troubles/156075>\
**Category:** Ruby\
**Created:** [December 20, 2008, 2:10am UTC](https://www.ruby-forum.com/t/yaml-troubles/156075 "2008-12-20T02:10:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [December 20, 2008, 2:10am UTC](https://www.ruby-forum.com/t/yaml-troubles/156075/1 "2008-12-20T02:10:28Z")

</div>

I am working on a ruby MUD server, and I wanted to save a list of  
characters in a separate YAML file.  
The YAML file should contain a hash for each character, being something  
like this:  
{“charactername” =\> [“password”, playerclassobject]}

I wrote up two simple little characters into the YAML file:

Test:

- soccer
- !ruby/object:Player  
id: :bob  
location: !ruby/object:Room  
contents:  
east:  
id: :center  
name: center  
north:  
south:  
west:  
name: bob

* * *

Zonbi:

- soccer
- !ruby/object:Player  
id: :zonbi  
location: !ruby/object:Room  
contents:  
east:  
id: :center  
name: center  
north:  
south:  
west:  
name: zonbi

Now when I try ‘a = YAML::load(File.open(‘names.yaml’))’, a only returns  
the first character set of data:

{“Test”=\>[“soccer”, #\<YAML::Object:0x60c674 @class=“Player”,  
@ivars={“name”=\>“bob”, “id”=\>:bob, “location”=\>#\<YAML::Object:0x60c8b8  
@class=“Room”, @ivars={“name”=\>“center”, “east”=\>nil, “id”=\>:center,  
“south”=\>nil, “west”=\>nil, “contents”=\>nil, “north”=\>nil}\>}\>]}

and doing ‘a[“Test”][1]’ gives:

#\<YAML::Object:0x60c674 @class=“Player”, @ivars={“name”=\>“bob”,  
“id”=\>:bob, “location”=\>#\<YAML::Object:0x60c8b8 @class=“Room”,  
@ivars={“name”=\>“center”, “east”=\>nil, “id”=\>:center, “south”=\>nil,  
“west”=\>nil, “contents”=\>nil, “north”=\>nil}\>}\>]}

Instead of just in …::Player:0x00000 or whatever it should be. Any  
help here?

Thanks in advanced, Timothy.

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [December 20, 2008, 4:00am UTC](https://www.ruby-forum.com/t/yaml-troubles/156075/2 "2008-12-20T04:00:18Z")

</div>

I don’t think I was clear on my actual questions:

- Why doesn’t the variable ‘a’ include BOTH of the character data?
- How do I fix it?
- Why does ‘a’ list the Player class as an array, and not actually as an  
object?

Thanks! ^\_^’

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [December 20, 2008, 4:09am UTC](https://www.ruby-forum.com/t/yaml-troubles/156075/3 "2008-12-20T04:09:34Z")

</div>

On 20.12.2008, at 02:52 , Tim M. wrote:

> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

I don’t have an answer to your question but when I needed YAML I found  
that it didn’t read it exactly as I expected so I made the objects I  
wanted to get inside ruby and had it dump the objects to YAML, then I  
got the right syntax. Try that.

einarmagnus

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [December 20, 2008, 4:16am UTC](https://www.ruby-forum.com/t/yaml-troubles/156075/4 "2008-12-20T04:16:55Z")

</div>

Einar MagnÃºs Boson wrote:

> On 20.12.2008, at 02:52 , Tim M. wrote:
> 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> I don’t have an answer to your question but when I needed YAML I found  
> that it didn’t read it exactly as I expected so I made the objects I  
> wanted to get inside ruby and had it dump the objects to YAML, then I  
> got the right syntax. Try that.
> 
> einarmagnus

I had been doing that, but ruby still doesn’t read it up from the YAML  
correctly. Maybe I’ll make my own parsing engine for that kind of  
stuff… (_starts formulating all kind of wacked out, super groovy,  
super cool ideas that will never come to be…_)

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [December 20, 2008, 4:44am UTC](https://www.ruby-forum.com/t/yaml-troubles/156075/5 "2008-12-20T04:44:04Z")

</div>

On Dec 19, 2008, at 10:08 PM, Tim M. wrote:

> > wanted to get inside ruby and had it dump the objects to YAML, then I  
> > got the right syntax. Try that.
> > 
> > einarmagnus
> 
> I had been doing that, but ruby still doesn’t read it up from the YAML  
> correctly. Maybe I’ll make my own parsing engine for that kind of  
> stuff… (_starts formulating all kind of wacked out, super groovy,  
> super cool ideas that will never come to be…_)

Just use the right method:  
YAML.each\_document  
YAML.load\_documents

details from your friendly neighborhood `ri`

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![enchanter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/ce7236/32.png) [@enchanter](https://www.ruby-forum.com/u/enchanter)\
**Post date:** [December 20, 2008, 10:32am UTC](https://www.ruby-forum.com/t/yaml-troubles/156075/6 "2008-12-20T10:32:48Z")

</div>

class Player  
def initialize(name, id)  
@name, @id = name, id  
end  
end

players = {  
“bob” =\> Player.new(:bob, “bob”),  
“zonbi” =\> Player.new(:zonbi, “zonbi”),  
}

require “yaml”  
File.open(“names.yaml”,“wb”) { |f| YAML.dump(players, f) }

p2 = File.open(“names.yaml”,“rb”) { |f| YAML.load(f) }  
puts p2.size  
p p2

## This gives the following format for the players file:

zonbi: !ruby/object:Player  
id: zonbi  
name: :zonbi  
bob: !ruby/object:Player  
id: bob  
name: :bob

I think the problem is the extra “—” you inserted in between the  
players.
