【发布时间】:2014-08-23 19:39:40
【问题描述】:
这是我的代码:
require 'yaml'
class Person
attr_accessor :name, :age
end
yaml_string = <<END_OF_DATA
---
-!ruby/object:Person
age: 45
name: Jimmy
- !ruby/object:Person
age:23
name: Laura Smith
END_OF_DATA
test_data = YAML::load(yaml_string)
puts test_data[0].name
puts test_data[1].name
这是我得到的结果:
ruby yaml1.rb C:/Ruby200/lib/ruby/2.0.0/psych.rb:205:in
parse': (<unknown>): mapping values are not allowed in this context at line 3 column 4 (Psych::SyntaxError) from C:/Ruby200/lib/ruby/2.0.0/psych.rb:205:inparse_stream' 来自 C:/Ruby200/lib/ruby/2.0.0/psych.rb:153:inparse' from C:/Ruby200/lib/ruby/2.0.0/psych.rb:129:inload' 来自 yaml1.rb:17:in `' 退出代码:1
根据我正在阅读的书(Peter Cooper 的 Begging Ruby)。我的结果应该如下所示:
Jimmy
Laura Smith
有人知道为什么会这样吗?我做错了什么?
【问题讨论】: