Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法告诉ruby中的Psych使用内联模式?_Ruby_Yaml_Psych - Fatal编程技术网

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

有没有办法告诉ruby中的Psych使用内联模式?,ruby,yaml,psych,Ruby,Yaml,Psych,环境:ruby1.9.3,psych(任何版本) 例: 实际结果: --- hash: name: Steve foo: bar 预期产出: --- hash: { name: Steve, foo: bar } Psych支持这一点,尽管它一点也不简单 我已经开始研究这个了 我最终设计了特定的对象,包括内联散列和数组 使用我的脚本,您的问题的解决方案是: o = { 'hash' => StyledYAML.inline('name' => 'Steve', 'foo

环境:ruby1.9.3,psych(任何版本) 例:

实际结果:

---
hash:
  name: Steve
  foo: bar
预期产出:

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

Psych支持这一点,尽管它一点也不简单

我已经开始研究这个了

我最终设计了特定的对象,包括内联散列和数组

使用我的脚本,您的问题的解决方案是:

o = { 'hash' => StyledYAML.inline('name' => 'Steve', 'foo' => 'bar') }
StyledYAML.dump o, $stdout
gem以一种方便的OOP风格提供了这一点

考虑到您有一个模型用户:

user.name => "Andrew"
user.age => "over 18"
现在,您需要定义一个representer模块来呈现/解析用户实例

require 'representable/yaml'

module UserRepresenter
  include Representable::YAML

  collection :hash, :style => :flow

  def hash
    [name, age]
  end
end
定义YAML文档后,只需扩展用户实例并呈现它

user.extend(UserRepresenter).to_yaml
#=> ---
hash: [Andrew, over 18]

希望能有帮助,安德鲁

我怀疑Psych是否支持这一点。LibYAML或Psych源代码中的
ack-i内联
所产生的唯一结果是内联数组和散列的解析测试。为什么需要这个?因为我的散列非常像表格数据。我想看得更清楚些,节省显示器的空间。我想那你就走运了。但您可以将其作为CSV或其他文件写入单独的文件中进行检查。谢谢。实际上,我有一个具有层次散列和数组结构的对象。我想把它转换成一种文本格式(比如YAML)。为什么不把它另存为YAML,编写一个小的ruby脚本来解析和格式化它,这样你就可以很容易地检查它了?这可以在几分钟内完成,如果不是几秒钟的话。嗨,对不起。如何在我的rails应用程序中包含styled_yaml.rb?谢谢。把它放在某个地方(例如“lib/”),然后需要它。
user.extend(UserRepresenter).to_yaml
#=> ---
hash: [Andrew, over 18]