gqli
gqli

Reputation: 1045

Why i'm getting Psych::SyntaxError while translating YAML data back into working objects

Ruby version: ruby 2.0.0p576 (2014-09-19 revision 47628) [x86_64-darwin13.4.0] I'm reading "beginning ruby" book and get stuck at translating YAML data back into working objects. (it's worth to mention that while converting working objects into YAML data works fine) Please see code blow:

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

error occurs

2.0.0-p576 :013"> END_OF_DATA
 => "---\n- !ruby/object:Person\nage: 45\nname: Jimmy\n- !ruby/object:Person\nage: 23\nname: Laura Smith\n" 
2.0.0-p576 :014 > test_data = YAML::load(yaml_string)
Psych::SyntaxError: (<unknown>): did not find expected '-' indicator while parsing a block collection at line 2 column 1

I have done some research, intuitively,i think this link click here is saying something about this issue . However , I have no idea of what are they talking about due to my "kindergarten" level of ruby language. Wish someone can help me solve this problem and understand it completely . Thanks in advance !

Upvotes: 2

Views: 168

Answers (1)

Doydle
Doydle

Reputation: 921

You're missing some indentation. YML isn't white space agnostic. Try using this data instead:

yaml_string = <<END_OF_DATA
---
- !ruby/object:Person
  age: 45
  name: Jimmy
- !ruby/object:Person
  age: 23
  name: Laura Smith
END_OF_DATA

Upvotes: 3

Related Questions