From: transfire@... Date: 2006-06-05T04:17:53+09:00 Subject: Re: Hash to OpenStruct (#81) I manged a very small solution -- practically one line. Only problem is, it doesn't work ;-) But honestly, it's not my fault! No really. Let me explain. When I first read the quiz my thoughts intitally went to the usual concepts and I considered the Hash#traverse method I wrote some time ago (BTW this quiz helped me improve that method. Many thanks!) But I have good bit of experience with YAML and I immediately had a second thought which would allow me to solve the quiz very quickily and easily. The solution is as follows (were s containes the yaml sample). YAML.add_builtin_type('map'){ |t,v| OpenStruct.new(v) }; o = YAML.load(s) But like I said, as clever as it may be, it doesn't work. For whatever reason Syck doesn't handle it properly. Perhaps YAML's 'map' type is too fundamental that it can't comply, or perhaps it's a bug. I don't know. But it just end up returning the same old Hash. Okay I thought. There's more than one way to skin a cat. And I came up with this close to one-liner that works around the above problem in a most clever way. i = YAML::load(s) def Hash.def to_yaml_type "!ruby/object:OpenStruct" end o = YAML::load(i.to_yaml) The nice thing about this soluiton is that it uses a built-in library (YAML/Syck) to do all the hard work --since Syck already understands graphs it takes care of all those messy issues. Cool. T.