Gotta post this before I look at other solutions. This caused me to
look up what an OpenStruct was so that was benefit #1. Since it was
a simple one, I worked through it with my son who is going through
Chris Pine's _Learning_to_Program_ right now so that was benefit #2.
I don't know if this will handle the crazier recursive YAML files,
but it seems to be fine for normal ones. My son actually struck on
the OpenStruct#send being a problem with the presence of the 'send'
key in the YAML.
-Rob
# RubyQuiz81: Hash to OpenStruct
# 2006-06-02
require 'ostruct'
require 'yaml'
class HashToOpenStruct
def self.from_yaml(yamlfile)
self.to_ostruct(YAML.load(File.open(yamlfile)))
end
def self.to_ostruct(h)
c = OpenStruct.new
h.each { |k,v| c.__send__("#{k}=".to_sym,
v.kind_of?(Hash) ? to_ostruct(v) : v) }
c
end
end
__END__
Rob Biedenharn http://agileconsultingllc.com
Rob / AgileConsultingLLC.com
+1 513-295-4739