End of Yaml

Is there a way to have something like this in a yaml file:

foo: bar
__ STOP TREATING IT AS YAML FILE FROM HERE __
\²\²\24²¼ljk
asdjladskasd
some noise is here and there
asdiasdiodas
KJLljkq @@@@j€ pqwjpqowj qewopjewqpjewqpjwqp728879241421ü*24242
__ 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?

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

Marc H. wrote:

Man, I thought something terrible happened with YAML, with this kind of
subject. :slight_smile:

Is there a way to have something like this in a yaml file:

Comment the lines is an option?

foo: bar

__ STOP TREATING IT AS YAML FILE FROM HERE __

\²\²\24²¼ljk

asdjladskasd

some noise is here and there

asdiasdiodas

KJLljkq @@@@j€ pqwjpqowj qewopjewqpjewqpjwqp728879241421ü*24242

__ END OF YAML FILE __

Best regards,

On Aug 16, 2008, at 06:40 , Marc H. wrote:

__ END OF YAML FILE __
No, but you can do the opposite:

ruby_code
END
yaml

and access that with DATA.read

Michael T. Richter wrote:

This whole END/DATA thing does not work as expected. It’s yet
another situation where “principle of least surprise” doesn’t work at
all in Ruby. Consider:

DATA is only provided for the file immediately executed on the command
line. You read from DATA twice, so the second read is at EOF and returns
“”.

  • Charlie

On Mon, 2008-08-18 at 16:32 +0900, Charles Oliver N. wrote:

Michael T. Richter wrote:

This whole END/DATA thing does not work as expected. It’s yet
another situation where “principle of least surprise” doesn’t work at
all in Ruby. Consider:

DATA is only provided for the file immediately executed on the command
line. You read from DATA twice, so the second read is at EOF and returns “”.

I am aware of this. My point is that relying on END/DATA for
anything more than the most trivial of things is a recipe for getting
bitten in the ass. Hard. If your code requires another file and that
file consumes the DATA item, you’re starved. If your code is required
by another file then your DATA item vanishes and is replaced by that
code’s DATA item.

Basically, absent a proper module system in Ruby, END/DATA is a toy
and not suitable for use in anything resembling production code.

On Aug 17, 2008, at 22:43 , Michael T. Richter wrote:

This whole END/DATA thing does not work as expected.

correction: doesn’t work as YOU expected… As said many times before
POLS is relative to Matz, not you or anyone else.

Thank you very much for thread hijacking, but I don’t find anything
you’re saying relevant to OP’s original problem. I’ve seen you rant
left and right before. I don’t think it has a place on #ruby-talk.

On Mon, 2008-08-18 at 14:12 +0900, Ryan D. wrote:

Is there a way to have something like this in a yaml file:
foo: bar
__ STOP TREATING IT AS YAML FILE FROM HERE __
\²\²\24²¼ljk
asdjladskasd
some noise is here and there
asdiasdiodas
KJLljkq @@@@j€ pqwjpqowj qewopjewqpjewqpjwqp728879241421ü*24242
__ END OF YAML FILE __

No, but you can do the opposite:
ruby_code
END
yaml

and access that with DATA.read

This whole END/DATA thing does not work as expected. It’s yet
another situation where “principle of least surprise” doesn’t work at
all in Ruby. Consider:

$ cat test1.rb
#test1.rb
p DATA.read
END
This is the DATA stuff from test1.rb.

$ cat test2.rb
#test2.rb
require ‘test1’
p DATA.read
END
This is the DATA from test2.rb.

$ ruby test2.rb
“This is the DATA from test2.rb.\n”
“”

The source of the problem is that Ruby doesn’t have a real system of
modules and namespaces. All require does is bring a chunk of text into
the current file being processed very much the same way that C’s
#include does.