ruby - 有没有办法告诉 Ruby 中的 Psych 使用内联模式?

标签 ruby yaml psych

环境: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/

相关文章:

mysql - has_and_belongs_to_many 权限过滤

amazon-web-services - Cloudformation - 在 map 中定义默认值

yaml - 是 .yaml 还是 .yml?

r - 使用 psych 包更改 R 图的主标题时出错

R psych tetrachoric - 二分变量

ruby - 试图找到 : . (eval):289 的文件/行:警告:不要在参数括号前放置空格

javascript - 在 Rails 4.1.4 中动态更改 Div 内容

amazon-web-services - 创建 CloudFormation 模板以创建多个 Glue 连接

ruby - Psych to_yaml 选项的文档?

arrays - 如何在 Rails 4.2 表单中提交字符串数组