我用以下代码创建了一个YAML结构:
File.open("systeminfohalf.yaml").each do |resp_yaml|
begin
systeminfo << YAML.parse(resp_yaml)
rescue Psych::SyntaxError
puts "skip ln #{resp_yaml}".yellow.bold
end
end
之所以这样做,是因为在Windows上,当我调用“ systeminfo”时,YAML很脏,因此我一次只能读取一行,然后丢弃这些脏行。这几乎是一个YAML过滤器。
然后,我尝试访问一个称为“系统类型”的元素
puts "architecture size #{systeminfo['System Type']}"
我收到以下错误:
.rb:34:in'[]':不会将String隐式转换为Integer(TypeError)
我检查了对象“ systeminfo”,它是一个数组。
以下是数组的一些元素:
#<Psych::Nodes::Document:0x0000000342d8a0 @children=[#<Psych::Nodes::Mapping:0x0000000342d850 @children=[#<Psych::Nodes::Scalar:0x0000000342d7d8 @value="System Manufacturer", @anchor=nil, @tag=nil, @p
lain=true, @quoted=false, @style=1>, #<Psych::Nodes::Scalar:0x0000000342d760 @value="", @anchor=nil, @tag=nil, @plain=true, @quoted=false, @style=1>], @anchor=nil, @tag=nil, @implicit=true, @style=1>]
, @version=[], @tag_directives=[], @implicit=true, @implicit_end=true>
===
#<Psych::Nodes::Document:0x0000000342d508 @children=[#<Psych::Nodes::Mapping:0x0000000342d4b8 @children=[#<Psych::Nodes::Scalar:0x0000000342d440 @value="System Model", @anchor=nil, @tag=nil, @plain=tr
ue, @quoted=false, @style=1>, #<Psych::Nodes::Scalar:0x0000000342d3f0 @value="", @anchor=nil, @tag=nil, @plain=true, @quoted=false, @style=1>], @anchor=nil, @tag=nil, @implicit=true, @style=1>], @vers
ion=[], @tag_directives=[], @implicit=true, @implicit_end=true>
===
#<Psych::Nodes::Document:0x0000000342d198 @children=[#<Psych::Nodes::Mapping:0x0000000342d148 @children=[#<Psych::Nodes::Scalar:0x0000000342d0d0 @value="System Type", @anchor=nil, @tag=nil, @plain=tru
e, @quoted=false, @style=1>, #<Psych::Nodes::Scalar:0x0000000342d058 @value="x64-based PC", @anchor=nil, @tag=nil, @plain=true, @quoted=false, @style=1>], @anchor=nil, @tag=nil, @implicit=true, @style
=1>], @version=[], @tag_directives=[], @implicit=true, @implicit_end=true>
“系统类型”在那里,我只是不知道如何获得它的价值。我认为我可以执行以下操作:
systeminfo["System Type"]
我在做什么错了?
以下是我所说的肮脏的YAML响应
Virtual Memory: Max Size: 18,708 MB
Virtual Memory: Available: 10,750 MB
Virtual Memory: In Use: 7,958 MB
在线YAML格式化程序对上述3行没有问题,但Ruby :: Psych却有问题。