简体   繁体   中英

How to style to_yaml correctly

I want to store some configuration information about a few apps so I can make an 'app drawer'. I store the info in an array that looks like this -

[["page_view", {"path"=>"somepath/asdf/asdf", "name"=>"Page View"}], ["outage_impact", {"path"=>"newpath/asdf/asdf", "name"=>"Outage Impact"}]]

but when I use to_yaml on this array I get this output:

--- 
- - page_view
  - path: somepath/asdf/asdf
    name: Page View
- - outage_impact
  - path: newpath/asdf/asdf
    name: Outage Impact 

Ideally I want something more like this:

page_view
  path: somepath/af/asdf
  name: blah
outage_impact
  name: blah
  path: adsf/adsf/asdf

I tried finding more info on the to_yaml method but it was few and far between. I think my array might need to be formatted differently but I have been guessing and checking for awhile to no avail.

Should I even be using the to_yaml method at all or would another method work better?

Converting it to a hash and then using to_yaml will give a similar output to your example (but with --- at the start)

arr = [["page_view", {"path"=>"somepath/asdf/asdf", "name"=>"Page View"}], ["outage_impact", {"path"=>"newpath/asdf/asdf", "name"=>"Outage Impact"}]]

puts Hash[*arr.flatten].to_yaml

gives:

---
page_view:
  path: somepath/asdf/asdf
  name: Page View
outage_impact:
  path: newpath/asdf/asdf
  name: Outage Impact

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM