# Emit YAML without leading hyphens?

**URL:** <https://www.ruby-forum.com/t/emit-yaml-without-leading-hyphens/164979>\
**Category:** Ruby\
**Created:** [April 7, 2009, 11:18pm UTC](https://www.ruby-forum.com/t/emit-yaml-without-leading-hyphens/164979 "2009-04-07T23:18:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [April 7, 2009, 11:18pm UTC](https://www.ruby-forum.com/t/emit-yaml-without-leading-hyphens/164979/1 "2009-04-07T23:18:44Z")

</div>

How to emit yaml without the leading hyphens?

```
[1,2,3].to_yaml
  # ==>"--- \n- 1\n- 2\n- 3\n"

```

I realize that I can do the following to get the result I’m after, but  
it seems sort of “after the fact”  
[1,2,3].to\_yaml[5…-1]  
# ==\>"- 1\n- 2\n- 3\n"

I know ‘to\_yaml’ takes options, but I can’t find acceptable options  
anywhere.  
[1,2,3].to\_yaml({:??? =\> true})

Thanks!

---

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [April 8, 2009, 12:28am UTC](https://www.ruby-forum.com/t/emit-yaml-without-leading-hyphens/164979/2 "2009-04-08T00:28:58Z")

</div>

On Apr 7, 2009, at 14:14 , bwv549 wrote:

> I know ‘to\_yaml’ takes options, but I can’t find acceptable options  
> anywhere.  
> [1,2,3].to\_yaml({:??? =\> true})

As I understand it, if you remove the hyphens, it is no longer yaml,  
so don’t do that.

---

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [April 8, 2009, 12:43am UTC](https://www.ruby-forum.com/t/emit-yaml-without-leading-hyphens/164979/3 "2009-04-08T00:43:01Z")

</div>

Ryan D. wrote:

> > [1,2,3].to\_yaml[5..-1]  
> > # ==\>“- 1\n- 2\n- 3\n”
> > 
> > I know ‘to\_yaml’ takes options, but I can’t find acceptable options  
> > anywhere.  
> > [1,2,3].to\_yaml({:??? =\> true})
> 
> As I understand it, if you remove the hyphens, it is no longer yaml, so  
> don’t do that.

Maybe so, but the yaml extension loads it anyway.

irb(main):001:0\> [1,2,3].to\_yaml[5..-1]  
=\> “- 1\n- 2\n- 3\n”  
irb(main):002:0\> YAML.load(\_)  
=\> [1, 2, 3]

---

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [April 8, 2009, 5:53pm UTC](https://www.ruby-forum.com/t/emit-yaml-without-leading-hyphens/164979/4 "2009-04-08T17:53:19Z")

</div>

Henrik H. wrote:

> Just a quick question: Why do you want to do this?

Can’t speak for bwv549, but when I’ve wanted to do this in the past it’s  
so that I can emit part of an array to the end of an existing YAML file  
such that it loads as a single YAML document at a later stage.

–  
Alex

---

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [April 9, 2009, 11:00pm UTC](https://www.ruby-forum.com/t/emit-yaml-without-leading-hyphens/164979/5 "2009-04-09T23:00:44Z")

</div>

> Can’t speak for bwv549, but when I’ve wanted to do this in the past it’s  
> so that I can emit part of an array to the end of an existingYAMLfile  
> such that it loads as a singleYAMLdocument at a later stage.

exactly the situation.

---

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [April 8, 2009, 12:03pm UTC](https://www.ruby-forum.com/t/emit-yaml-without-leading-hyphens/164979/6 "2009-04-08T12:03:59Z")

</div>

Just a quick question: Why do you want to do this?

Regards,  
Henrik H.

---

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [April 10, 2009, 8:39am UTC](https://www.ruby-forum.com/t/emit-yaml-without-leading-hyphens/164979/7 "2009-04-10T08:39:15Z")

</div>

On Thu, Apr 9, 2009 at 11:00 PM, bwv549 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Can’t speak for bwv549, but when I’ve wanted to do this in the past it’s  
> > so that I can emit part of an array to the end of an existingYAMLfile  
> > such that it loads as a singleYAMLdocument at a later stage.
> 
> exactly the situation.

I would stick with the “after the fact” changes to the YAML output as  
tacking on YAML from one array onto the output of a different YAML  
file is a very specific use-case.

Andrew T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

“I have never let my schooling interfere with my education” - Mark Twain

---

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [April 10, 2009, 12:58pm UTC](https://www.ruby-forum.com/t/emit-yaml-without-leading-hyphens/164979/8 "2009-04-10T12:58:47Z")

</div>

My problem with those hyphens is that a yaml file without hyphens is  
perfectly valid yaml (ruby yaml loads it happily) - i simply dont  
understand why those hyphens are created, when the yaml file _without_  
hyphens works perfectly nice.

---

<div class="post-metadata">

**Author:** ![bwv549](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ac8455/32.png) [@bwv549](https://www.ruby-forum.com/u/bwv549)\
**Post date:** [April 10, 2009, 4:42pm UTC](https://www.ruby-forum.com/t/emit-yaml-without-leading-hyphens/164979/9 "2009-04-10T16:42:27Z")

</div>

> perfectly valid yaml (ruby yaml loads it happily) - i simply dont  
> understand why those hyphens are created, when the yaml file _without_  
> hyphens works perfectly nice.

One reason for the hyphens is to allow multiple YAML documents (ie  
separate objects) to be stored in a single file:

docs = [1,2,3].to\_yaml + [4,5,6].to\_yaml

results =   
YAML.load\_documents(docs) do |doc|  
results \<\< doc  
end  
puts results.inspect # =\> “[[1,2,3], [4,5,6]]”

Notice each of the arrays is loaded separately. As for removing the  
header, take a look at this post:

[http://www.arkanis-development.de/weblog/2007/6/20/options-for-rubys-@to\_yaml@-method](http://www.arkanis-development.de/weblog/2007/6/20/options-for-rubys-@to_yaml@-method)

The author was facing the same problem. The option is {:UseHeader =\>  
false} but the to\_yaml options aren’t currently used and don’t work,  
even though at some point they probably did. It’s a deficiency in the  
current release.
