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?

···

--
Posted via http://www.ruby-forum.com/.

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

···

On Sat, Aug 16, 2008 at 8:40 AM, Marc Heiler <shevegen@linuxmail.org> 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 __

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?

Marc Heiler 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,

No, but you can do the opposite:

ruby_code
__END__
yaml

and access that with DATA.read

···

On Aug 16, 2008, at 06:40 , Marc Heiler 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 __

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.

···

On Mon, 2008-08-18 at 14:12 +0900, Ryan Davis 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

--
Michael T. Richter <ttmrichter@gmail.com> (GoogleTalk:
ttmrichter@gmail.com)
A well-designed and humane interface does not need to be split into
beginner and expert subsystems. (Jef Raskin)

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

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 Aug 17, 2008, at 22:43 , Michael T. Richter wrote:

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

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 Mon, 2008-08-18 at 16:32 +0900, Charles Oliver Nutter 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 "".

--
Michael T. Richter <ttmrichter@gmail.com> (GoogleTalk:
ttmrichter@gmail.com)
I'm not schooled in the science of human factors, but I suspect surprise
is not an element of a robust user interface. (Chip Rosenthal)