Created
February 8, 2011 23:33
-
-
Save buddylindsey/817528 to your computer and use it in GitHub Desktop.
Loading YAML from a file Problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem with this code is line 19 is converting yaml to yaml instead of an object to yaml.