On Sat, Aug 16, 2008 at 8:40 AM, Marc H. [email protected]
wrote:
__ END OF YAML FILE __
In other words, have a file that is a yaml file in the beginning and
then from a keyword or similar, to treat it as a “special” file with
arbitrary data, i.e. no matter if one puts images into it or something?
Probably not. In reading the YAML spec I find no way to indicate such
a thing. (http://yaml.org/spec/1.2/)
However, in Ruby, YAML::load only loads the first document in the
stream/string, so you can get away with something like the following:
irb(main):001:0> doc = "
irb(main):002:0" —
irb(main):003:0" foo: bar
irb(main):004:0" baz: [1, 2, 4, 8]
irb(main):005:0" …
irb(main):006:0" this stuff is not YAML
irb(main):007:0" "
=> “\n—\nfoo: bar\nbaz: [1, 2, 4, 8]\n…\nthis stuff is not YAML\n”
irb(main):008:0> YAML.load(doc)
=> {“baz”=>[1, 2, 4, 8], “foo”=>“bar”}
the ‘…’ line indicates the end of the document, and as you can see
everything following that line is not parsed.
But YAML::load_documents will treat the second bit as valid YAML,
because it is, after all, a valid YAML scalar:
irb(main):014:0> YAML.load_documents(doc){|y| p y}
{“baz”=>[1, 2, 4, 8], “foo”=>“bar”}
“this stuff is not YAML”
So the question really gets down to whether you can expect your files
to have only one YAML document at the head of the file. If so,
YAML::load combined with a ‘…’ could meet your needs.
If not, you may want to decide on a tag yourself and preparse the file
into YAML/not-YAML yourself before passing something into YAML::load.
-Michael