From: Joey Date: 2006-06-05T02:55:42+09:00 Subject: Re: [QUIZ][SOLUTION] Hash to OpenStruct (#81) ------=_Part_32150_5457778.1149443739305 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline My solution was very simple: require 'yaml';require 'ostruct';def h(h)h.map{|k,v|h[k]=Hash\ ===v ?h(v):v};OpenStruct.new(h)end;puts h(YAML.load($<.read)) This can't deal with the recursion that MentalGuy(sorry for the wrong capitalisation!) posted. The following is basically the same code: class Hash def to_ostruct copy = {} each do |(key,value)| if value.class == Hash copy[key] = value.to_ostruct else copy[key] = value end end OpenStruct.new(copy) end end I tried to look into changing YAML.load to make OpenStruct's instead of hashes, but I soon gave up on that :) j`ey ------=_Part_32150_5457778.1149443739305--