YAML troubles


#1

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.


#2

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! ^_^’


#3

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


Posted via 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


#4

Einar Magnús Boson wrote:

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


Posted via 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…
)


#5

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
removed_email_address@domain.invalid


#6

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.