Skip to content

Instantly share code, notes, and snippets.

@buddylindsey
Created February 8, 2011 23:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buddylindsey/817528 to your computer and use it in GitHub Desktop.
Save buddylindsey/817528 to your computer and use it in GitHub Desktop.
Loading YAML from a file Problem
require 'yaml'
# setup an object to serialize with yaml
class Square
attr_accessor :width, :height, :bonus, :me
def initialize width, height
@width = width
@height = height
@bonus = ['yo',{:msg => 'YAML 4TW', :alert => 'I am an alert'}]
@me = self
end
end
# seriallize a square object for use below
serialized = Square.new(2,3).to_yaml
# save yaml to file
File.open('main.yaml', 'w') do |out|
YAML.dump(serialized, out)
end
# Out putting yaml when we serialize it in memory
puts 'YAML Output via inmemory converstion to YAML'
new_obj = YAML::load(serialized)
puts new_obj.width.to_s
puts new_obj.height.to_s
puts new_obj.bonus[0]
puts new_obj.bonus[1][:msg]
puts new_obj.bonus[1][:alert]
# Output when we pull from yaml file
puts "YAML Output pulled from a file"
new_obj = YAML.load_file('main.yaml')
puts new_obj.width.to_s
puts new_obj.height.to_s
puts new_obj.bonus[0]
puts new_obj.bonus[1][:msg]
puts new_obj.bonus[1][:alert]
@buddylindsey
Copy link
Author

The problem with this code is line 19 is converting yaml to yaml instead of an object to yaml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment