# Writing YAML files in Ruby: lack of pretty printing formatting options

**URL:** <https://www.ruby-forum.com/t/writing-yaml-files-in-ruby-lack-of-pretty-printing-formatting-options/243488>\
**Category:** Ruby\
**Created:** [November 17, 2015, 10:36am UTC](https://www.ruby-forum.com/t/writing-yaml-files-in-ruby-lack-of-pretty-printing-formatting-options/243488 "2015-11-17T10:36:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [November 17, 2015, 10:36am UTC](https://www.ruby-forum.com/t/writing-yaml-files-in-ruby-lack-of-pretty-printing-formatting-options/243488/1 "2015-11-17T10:36:42Z")

</div>

What do you think about it ?

> <https://stackoverflow.com/questions/33706670/writing-yaml-files-in-ruby-lack-of-pretty-printing-formatting-options/33710802#33710802>

---

<div class="post-metadata">

**Author:** ![solyaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@solyaris](https://www.ruby-forum.com/u/solyaris)\
**Post date:** [November 18, 2015, 9:43am UTC](https://www.ruby-forum.com/t/writing-yaml-files-in-ruby-lack-of-pretty-printing-formatting-options/243488/2 "2015-11-18T09:43:49Z")

</div>

Ciao Giorgio,

Most YAML libraries I’ve worked with don’t preserve formatting or  
comments. Some quick research turns up only one that does—and it’s for  
Python (ruamel.yaml). In my experience, YAML is great for  
human-friendly, machine-readable configuration files and not much else.  
It loses its allure the second you bring machine-writeability into the  
picture.

In the past, when I’ve needed to programmatically update a YAML file, I  
try to treat it like any other text file. That is, I’ll seek to the  
line(s) I want to edit, apply a regex substitution or what have you, and  
write out the updated file. If I have to do a lot of heavy lifting  
within the file and it makes more sense to manipulate the data directly,  
I resign myself to losing my preferred formatting, and put all of my  
comments in as data, i.e. instead of this:

```
# This file contains the configuration for yadda yadda yadda...
# Lorem ipsum dolor sit amet, consectetur adipiscing elit.
---
- apples # red
- oranges # orange
- bananas # yellow
...

```

I’ll do something like this:

```
---
README: |
    This file contains the configuration for yadda yadda yadda...
    Lorem ipsum dolor sit amet, consectetur adipiscing elit.
...
---
- item: apples
  comments: red
- item: oranges
  comments: orange
- item: bananas
  comments: yellow
...

```

I feel like I’m giving this answer a lot lately, but if you really need  
a YAML library for Ruby that preserves comments and formatting, it might  
be fun to write! 🙂

---

<div class="post-metadata">

**Author:** ![solyaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@solyaris](https://www.ruby-forum.com/u/solyaris)\
**Post date:** [November 18, 2015, 1:33pm UTC](https://www.ruby-forum.com/t/writing-yaml-files-in-ruby-lack-of-pretty-printing-formatting-options/243488/3 "2015-11-18T13:33:04Z")

</div>

Thanks for your answer, Mike  
You perfectly got the point with your example containings comments and  
pretty printed data.  
In facts key “comment” in my example is just to simulate a … comment  
as you did in your README key example.

Probably you are right: I could made “Yet Another YAML” gem myself 🙂  
a bit of parsing exercise (I’m not so good in parsing programming).  
And the reason I asked here is because I hope some one else already did  
a better gem 🙂

BTW, the “raugh” Ruby YAML implementation in 2016, seriously surprise me  
a bit, because, If I well remebers, few years ago, almost every Ruby  
tutorial for beginners as me, was starting with statement:  
YAML everywhere!  
YAML everywhere!  
YAML everywhere!  
(See Rails configs too, etc.)

Anyway, jokes a part: Yes, YAML is not perfect, but I’m still interested  
in YAML as shareable format comprensible by both humans and both  
machines 😉 Ok otherwise we have JSON, binary mixin like superb  
[messagepack.org](http://messagepack.org) and of course binary blobs data at the opposite extreme  
🙂

---

<div class="post-metadata">

**Author:** ![solyaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@solyaris](https://www.ruby-forum.com/u/solyaris)\
**Post date:** [November 18, 2015, 7:37pm UTC](https://www.ruby-forum.com/t/writing-yaml-files-in-ruby-lack-of-pretty-printing-formatting-options/243488/4 "2015-11-18T19:37:05Z")

</div>

Sure, YAML everywhere in Rails, but only for human-friendly,  
machine-readable configuration. Does Rails ever write back out to YAML?

---

<div class="post-metadata">

**Author:** ![solyaris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3ec8ea/32.png) [@solyaris](https://www.ruby-forum.com/u/solyaris)\
**Post date:** [November 19, 2015, 7:24am UTC](https://www.ruby-forum.com/t/writing-yaml-files-in-ruby-lack-of-pretty-printing-formatting-options/243488/5 "2015-11-19T07:24:51Z")

</div>

> Does Rails ever write back out to YAML?

No, I agree 🙂  
But this is not a good reason  
to do not write YAML files  
(I’m Rails oppositor, joking)
