Here, you save your yaml objects as Person objects and then when you load them back, they will load into Person objects, making them a lot easier to handle.
First change tweak your yaml file to something like this:
---
- !ruby/object:Person
name: John Doe
sname: jdoe
email: [email protected]
- !ruby/object:Person
name: Jane Doe
sname: jdoe
email: [email protected]
Now you can load your yaml file into an array of Person objects and then manipulate the array:
FILENAME = 'data.yaml'
class Person
attr_accessor :name, :sname, :email
end
require "yaml"
# Will return an array of Person objects.
data = YAML::load(File.open(FILENAME))
# Will print out the first object in the array's name. #=> John Doe
puts data.first.name