YAML troubles

T

Tim Mcd

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.
 
T

Tim Mcd

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! ^_^'
 
E

Einar Magnús Boson

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


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
 
T

Tim Mcd

Einar said:
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...*)
 
R

Rob Biedenharn

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 Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
B

Brian Candler

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top