Forum: Ruby YAML troubles

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
907f29cb608004d2156d5ce88240f09b?d=identicon&s=25 Tim Mcd (zonbi)
on 2008-12-20 02:10
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.
907f29cb608004d2156d5ce88240f09b?d=identicon&s=25 Tim Mcd (zonbi)
on 2008-12-20 04:00

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! ^_^'
87349a7a95b3f2e83c20194ef122885c?d=identicon&s=25 Einar Magnús Boson (Guest)
on 2008-12-20 04:09
(Received via mailing list)
On 20.12.2008, at 02:52 , Tim Mcd 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
907f29cb608004d2156d5ce88240f09b?d=identicon&s=25 Tim Mcd (zonbi)
on 2008-12-20 04:16
Einar Magnús Boson wrote:
> On 20.12.2008, at 02:52 , Tim Mcd 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...*)
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2008-12-20 04:44
(Received via mailing list)
On Dec 19, 2008, at 10:08 PM, Tim Mcd 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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2008-12-20 10:32
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.
This topic is locked and can not be replied to.