4

Considering writing/reading files in YAML format (http://yaml.org/)

I'm just surprised by apparent lack of output formatting options in default YAML.dump (Ruby 2.2.3). Without any pretty printing option, the YAML.dump appears really ugly. I explain:

Consider this hand-written YAML configuration file 'config/bots.yml' where I have a list of items (hashes, each one with keys 'token' and 'comment':

input file:

- token: 070743004:yuSJJdB5L354Zq41iGIggSdYGJ1IhVh8XSrA
  comment: ROSPOshop.com

- token: 998001334:zAFo4dBdd3ZZtqKiGdPqkkYGJ1ppVW8pUZ
  comment: pagoSALDO.com bot

- token: 184679990:BBBBBBBBBCCCCCCCGIDDDDDDDHHHHHHHHHH
  comment: SOLYARISoftware demo bot

- token: 184679990:BBBBBBBBBCCCCCCUUUUUUUUUUHHHHHHHHHH
  comment: Another demo bot

- token: 184679990:BBBBBBBBBCCCCCCCGHGGHHGHGHHHHHHHHHH
  comment: Yet Another demo bot

No elaboration: just a load and a successive dump script as:

config = YAML.load(File.open('config/bots.yml'))
File.open('config/bots.yml', "w") { |f| f.write(YAML.dump(config)) }

output file:

---
- token: 070743004:yuSJJdB5L354Zq41iGIggSdYGJ1IhVh8XSrA
  comment: ROSPOshop.com
- token: 998001334:zAFo4dBdd3ZZtqgKiGdPqkkYGJ1ppVW8pUZ
  comment: pagoSALDO.com bot
- token: 184679990:BBBBBBBBBCCCCCCCGIDDDDDDDHHHHHHHHHH
  comment: SOLYARISoftware demo bot
- token: 184679990:BBBBBBBBBCCCCCCUUUUUUUUUUHHHHHHHHHH
  comment: Another demo bot
- token: 184679990:BBBBBBBBBCCCCCCCGHGGHHGHGHHHHHHHHHH
  comment: Yet Another demo bot

I'm unhappy because all array items are now collapsed (the line break is removed). That's very sad if the numbers of items is long and/or data structures for each item isa variable: a messy reading!

Question (1) There is any YAML option do do some more pretty printing for YAML.dump ? By example to separate with a blank line each item in an Array ?

Question (2) I found this very helpfull tutorial ("YAML Cookbook"): http://www.yaml.org/YAML_for_ruby.html#yaml_for_ruby

There is any more recent update / official Ruby doc about explaining YAML tips&tricks (data conversions, etc.) ?

Question (3) Any possible YAML alternative ? I mean maybe an alternative gem to read/write YAML ? BTW, of course I considered JSON, but I prefer the more clear YAML format when reading texts data!

UPDATED BTW, A lot of info/useful YAML format tips here: https://en.wikipedia.org/wiki/YAML

1 Answer 1

4

You can write your own pretty-print solution, if that's all you're looking for. For example:

config = YAML.load(File.open('bots.yml'))
puts config.to_yaml.gsub("\n-", "\n\n-")

Output:

---

- token: 070743004:yuSJJdB5L354Zq41iGIggSdYGJ1IhVh8XSrA
  comment: ROSPOshop.com

- token: 998001334:zAFo4dBdd3ZZtqKiGdPqkkYGJ1ppVW8pUZ
  comment: pagoSALDO.com bot

- token: 184679990:BBBBBBBBBCCCCCCCGIDDDDDDDHHHHHHHHHH
  comment: SOLYARISoftware demo bot

- token: 184679990:BBBBBBBBBCCCCCCUUUUUUUUUUHHHHHHHHHH
  comment: Another demo bot

- token: 184679990:BBBBBBBBBCCCCCCCGHGGHHGHGHHHHHHHHHH
  comment: Yet Another demo bot
2
  • smart! You ar right -> accepted! Neverthless I complai about the YAML Ruby implementation. By example If I'm not wrong with YAML.load I loss comments line like --- # comment Commented Nov 15, 2015 at 10:49
  • Psych, the Ruby implementation, wraps LibYAML, a parser and emitter written in C. The LibYAML scanner strips out all whitespace and comments. Net result: Psych never sees comments and therefore cannot preserve them.
    – CraigM
    Commented Nov 15, 2015 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.