YAML questions

Since I notice _why is around, I’ll ask a couple of things
that have been on my mind.

First of all, are there guidelines somewhere for writing YAML
manually? I can’t figure out how to put hashes/arrays in a
single line instead of multiple lines. (I got the idea this was
possible somehow.)

Also, why don’t we have #load_file and #dump_file or something
of that nature? I frequently find myself doing:

obj = nil
File.open("filename") {|f| obj = YAML.load(f) }

when I want to do:

obj = YAML.load_file("filename")

Just a thought.

Hal

Hal Fulton wrote:

First of all, are there guidelines somewhere for writing YAML
manually? I can’t figure out how to put hashes/arrays in a
single line instead of multiple lines. (I got the idea this was
possible somehow.)

irb(main):017:0> YAML.load(“—\n [1, 2, 3]”)
=> [1, 2, 3]
irb(main):018:0> YAML.load(“—\n {1: 2, 3: 4}”)
=> {1=>2, 3=>4}

obj = YAML.load_file(“filename”)

That looks very nice and I would find it nice, too.

(I’ve been using YAML.load(File.read(filename)) for this which isn’t all
that comfortable…)

Oh, and here’s something I find quite confusing about yaml.rb:

It’s possible to do YAML.load(“- foo”), but YAML.load(“[foo]”) produces
a parse error. (YAML.load(“—\n[foo]”) also works.)

Regards,
Florian Gross

Hal Fulton wrote:

First of all, are there guidelines somewhere for writing YAML
manually? I can’t figure out how to put hashes/arrays in a
single line instead of multiple lines. (I got the idea this was
possible somehow.)

you’ve seen the Cookbook? [1] see the section on inline collections [2]
for examples. yikes, i just noticed this is missing from the docs. [3]
i will fix.

Also, why don’t we have #load_file and #dump_file or something
of that nature? I frequently find myself doing:

obj = nil
File.open(“filename”) {|f| obj = YAML.load(f) }

YAML::load and YAML::dump both accept IO objects of any kind:

obj = YAML::load( File.open( “filename” ) )

to dump:

YAML::dump( obj, File.open( “filename2” ) )

follows Marshal’s API.

please ask all you like, i’m interested to know where documentation is
lacking (working on ri docs today) and what is tripping up any of you
from slathering your scripts in YAML.

_why

[1] http://yaml4r.sourceforge.net/cookbook/
[2] http://yaml4r.sourceforge.net/cookbook/#inline_collections
[3] http://yaml4r.sourceforge.net/doc/

Florian Gross wrote:

It’s possible to do YAML.load(“- foo”), but YAML.load(“[foo]”) produces
a parse error. (YAML.load(“—\n[foo]”) also works.)

yes, historically, root-level inline collections needed a document
header. but a recent version of the YAML specification does allow this.

i’ve fixed this in syck cvs. rolling over to ruby 1.9 shortly. lovely
catch.

_why

why the lucky stiff wrote:

Hal Fulton wrote:

Also, why don’t we have #load_file and #dump_file or something
of that nature? I frequently find myself doing:

obj = nil
File.open(“filename”) {|f| obj = YAML.load(f) }

YAML::load and YAML::dump both accept IO objects of any kind:

obj = YAML::load( File.open( “filename” ) )

to dump:

YAML::dump( obj, File.open( “filename2” ) )

follows Marshal’s API.

Yes, I see that. It’s just that

  1. I don’t really want to explicitly do File.open or File.read
    all the time
  2. In the case of #open above, note that the file is not closed
    until GC (correct?)

Personally, I want methods that keep the File stuff enclosed inside.
I have my own, and they’re trivial, but you know how it is. :slight_smile:

Actually, I wouldn’t mind it for Marshal either, but that sounds
more like an RCR (instead of a _wCR).

And thanks for the inline collections ref.

Cheers,
Hal