Yaml nuby question - duplicate keys

Hi all,

Ruby 1.8.2

I'd like to have a config file that looks like this:

# test.yml
foo:
   user: usr1
   password: pwd1
foo:
   user: usr2
   password: pwd2
bar:
   user: usr3
   password: pwd3

But, I try a simple YAML.load(File.open("test.yml") I lose one of the
entries, because it loads it as a Hash which, of course, does not
allow for duplicate keys.

Is there a way I can keep this kind of config file and still use YAML?
I looked at the various parse() options, but it started to go over my
head at that point.

Thanks.

Dan

Daniel Berger wrote:

Hi all,

Ruby 1.8.2

I'd like to have a config file that looks like this:

# test.yml
foo:
   user: usr1
   password: pwd1
foo:
   user: usr2
   password: pwd2
bar:
   user: usr3
   password: pwd3

But, I try a simple YAML.load(File.open("test.yml") I lose one of the
entries, because it loads it as a Hash which, of course, does not
allow for duplicate keys.

Is there a way I can keep this kind of config file and still use YAML?
I looked at the various parse() options, but it started to go over my
head at that point.

Try this (off the top of my head)
  # test.yml
- foo:
     user: usr1
     password: pwd1
- foo:
     user: usr2
     password: pwd2
- bar:
     user: usr3
     password: pwd3

This should give you a list of hashes; your code will have to do the merge to join up common items

James

···

Thanks.

Dan

.

Hi --

Hi all,

Ruby 1.8.2

I'd like to have a config file that looks like this:

# test.yml
foo:
   user: usr1
   password: pwd1
foo:
   user: usr2
   password: pwd2
bar:
   user: usr3
   password: pwd3

But, I try a simple YAML.load(File.open("test.yml") I lose one of the
entries, because it loads it as a Hash which, of course, does not
allow for duplicate keys.

Is there a way I can keep this kind of config file and still use YAML?
I looked at the various parse() options, but it started to go over my
head at that point.

You could store them as an array of hashes, if that doesn't interfere
too much with what you're doing on the non-YAML side.

I'm not too well-versed in the parse/stream stuff either... but I
think you can maintain two distinct hashes pretty easily like this:

require 'yaml'

a = { "foo" => { "user" => "user1", "password" => "password1" } }
b = { "foo" => { "user" => "user2", "password" => "password2" } }

y = YAML.dump_stream(a,b) # a multi-object "to_yaml" effect
l = YAML.load_stream(y)

a = l[0]
b = l[1]

p a, b

__END__

The only thing here where I feel like I must be missing something is
the explicit indexing part (l[0], l[1]). I rather expected the stream
object to be enumerable. Over to someone who knows more....

David

···

On Wed, 8 Sep 2004, Daniel Berger wrote:

--
David A. Black
dblack@wobblini.net

Daniel Berger wrote:

Hi all,

Ruby 1.8.2

I'd like to have a config file that looks like this:

# test.yml
foo:
   user: usr1
   password: pwd1
foo:
   user: usr2
   password: pwd2
bar:
   user: usr3
   password: pwd3

But, I try a simple YAML.load(File.open("test.yml") I lose one of the
entries, because it loads it as a Hash which, of course, does not
allow for duplicate keys.

Is there a way I can keep this kind of config file and still use YAML?
I looked at the various parse() options, but it started to go over my
head at that point.

You can try turning into an array, instead of a hash:

- foo:
     user: usr1
     password: pwd1
- foo:
     user: user2
     password: pwd2

Then you'd access it like:

   data[0]['foo']['user']

instead of

   data['foo']['user']

Hope that helps,

Jamis

···

Thanks.

Dan

.

--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck.org/jamis

"I use octal until I get to 8, and then I switch to decimal."

Daniel Berger wrote:

I'd like to have a config file that looks like this:

# test.yml
foo:
   user: usr1
   password: pwd1
foo:
   user: usr2
   password: pwd2
bar:
   user: usr3
   password: pwd3

It looks like you're trying to emulate XML, nnn? Yeah, the above isn't allowed in the YAML specification.

I often use type tagging to emulate XML:

   - !!foo
     user: usr1
     password: pwd1
   - !!foo
     user: usr2
     password: pwd2
   - !!bar
     user: usr3
     password: pwd3

I should write up a tutorial on how to do this. Here's the basic reference: http://yaml4r.sourceforge.net/doc/page/type_families.htm\.

_why

why the lucky stiff <ruby-talk@whytheluckystiff.net> wrote in message news:<413E4D02.9010909@whytheluckystiff.net>...

Daniel Berger wrote:
>
> I'd like to have a config file that looks like this:
>
> # test.yml
> foo:
> user: usr1
> password: pwd1
> foo:
> user: usr2
> password: pwd2
> bar:
> user: usr3
> password: pwd3
>

It looks like you're trying to emulate XML, nnn? Yeah, the above isn't
allowed in the YAML specification.

I often use type tagging to emulate XML:

   - !!foo
     user: usr1
     password: pwd1
   - !!foo
     user: usr2
     password: pwd2
   - !!bar
     user: usr3
     password: pwd3

I should write up a tutorial on how to do this. Here's the basic
reference: http://yaml4r.sourceforge.net/doc/page/type_families.htm\.

_why

Thanks Why, James, Jamis, and David for all your responses.

Dan