[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

YAML troubles

Tim Mcd

12/20/2008 1:02:00 AM

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.
--
Posted via http://www.ruby-....

5 Answers

Tim Mcd

12/20/2008 2:52:00 AM

0



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! ^_^'
--
Posted via http://www.ruby-....

Einar Magnús Boson

12/20/2008 3:01:00 AM

0


On 20.12.2008, at 02:52 , Tim Mcd wrote:

>
>
> 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! ^_^'
> --
> Posted via http://www.ruby-....
>


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




Tim Mcd

12/20/2008 3:09:00 AM

0

Einar Magnús Boson wrote:
> On 20.12.2008, at 02:52 , Tim Mcd wrote:
>
>> --
>> Posted via http://www.ruby-....
>>
>
>
> 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...*)
--
Posted via http://www.ruby-....

Rob Biedenharn

12/20/2008 3:35:00 AM

0


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

> Einar Magn=FAs Boson wrote:
>> On 20.12.2008, at 02:52 , Tim Mcd wrote:
>>
>>> Now when I try 'a =3D YAML::load(File.open('names.yaml'))', a only =20=

>>> returns
>>> the first character set of data:
>>
>> I don't have an answer to your question but when I needed YAML I =20
>> 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...*)


Just use the right method:
YAML.each_document
YAML.load_documents

details from your friendly neighborhood `ri`

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



Brian Candler

12/20/2008 9:25:00 AM

0

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.
--
Posted via http://www.ruby-....