0

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?

Josh
  • 5,631
  • 1
  • 28
  • 54
  • It seems to be serializing what you have. You'll need ":" following page_view and output_impact in your expected output. If you want the latter, could your source data be something like {"page_view": {"path": "somepath/...", ... }, "outage_impact": {...} } – clarkevans Mar 02 '12 at 17:17

1 Answers1

1

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
pjumble
  • 16,880
  • 6
  • 43
  • 51