Search code examples
rubyyamlpsych

Is there a way to tell Psych in ruby to using inline mode?


environment: ruby1.9.3 , psych(any version) ex:

o = { 'hash' => { 'name' => 'Steve', 'foo' => 'bar' } } 
 => {"hash"=>{"name"=>"Steve", "foo"=>"bar"}} 

#is there a inline option?
puts Psych.dump(o,{:inline =>true})

real result:

---
hash:
  name: Steve
  foo: bar

expect output:

--- 
hash: { name: Steve, foo: bar }

Solution

  • Psych supports this, although it isn't at all straightforward.

    I've started researching this in my own question on how to dump strings using literal style.

    I ended up devising a complete solution for setting various styles for specific objects, including inlining hashes and arrays.

    With my script, a solution to your problem would be:

    o = { 'hash' => StyledYAML.inline('name' => 'Steve', 'foo' => 'bar') }
    StyledYAML.dump o, $stdout