Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 251
Giorgio Robino Avatar asked Oct 27 '25 12:10

Giorgio Robino


1 Answers

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
like image 153
CraigM Avatar answered Oct 30 '25 05:10

CraigM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!