环境:ruby1.9.3,psych(任意版本) 例如:
o = { 'hash' => { 'name' => 'Steve', 'foo' => 'bar' } }
=> {"hash"=>{"name"=>"Steve", "foo"=>"bar"}}
#is there a inline option?
puts Psych.dump(o,{:inline =>true})
实际结果:
---
hash:
name: Steve
foo: bar
期望输出:
---
hash: { name: Steve, foo: bar }
最佳答案
Psych 支持这一点,尽管它一点也不简单。
我已经开始在 my own question on how to dump strings using literal style 中对此进行研究.
我最终设计了 a complete solution for setting various styles针对特定对象,包括内联哈希和数组。
使用我的脚本,您的问题的解决方案是:
o = { 'hash' => StyledYAML.inline('name' => 'Steve', 'foo' => 'bar') }
StyledYAML.dump o, $stdout
关于ruby - 有没有办法告诉 Ruby 中的 Psych 使用内联模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9556546/