# End of Yaml

**URL:** <https://www.ruby-forum.com/t/end-of-yaml/146036>\
**Category:** Ruby\
**Created:** [August 16, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/end-of-yaml/146036 "2008-08-16T15:43:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [August 16, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/end-of-yaml/146036/1 "2008-08-16T15:43:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [August 16, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/end-of-yaml/146036/2 "2008-08-16T16:10:21Z")

</div>

On Sat, Aug 16, 2008 at 8:40 AM, Marc H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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/](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

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [August 16, 2008, 4:13pm UTC](https://www.ruby-forum.com/t/end-of-yaml/146036/3 "2008-08-16T16:13:51Z")

</div>

Marc H. wrote:

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

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

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [August 18, 2008, 7:15am UTC](https://www.ruby-forum.com/t/end-of-yaml/146036/4 "2008-08-18T07:15:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [August 18, 2008, 9:37am UTC](https://www.ruby-forum.com/t/end-of-yaml/146036/5 "2008-08-18T09:37:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [August 18, 2008, 9:46am UTC](https://www.ruby-forum.com/t/end-of-yaml/146036/6 "2008-08-18T09:46:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [August 20, 2008, 1:53am UTC](https://www.ruby-forum.com/t/end-of-yaml/146036/7 "2008-08-20T01:53:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [August 18, 2008, 7:46am UTC](https://www.ruby-forum.com/t/end-of-yaml/146036/8 "2008-08-18T07:46:59Z")

</div>

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.
