From: Ola Bini Date: 2006-06-07T19:22:53+09:00 Subject: Re: [QUIZ][SOLUTION] Hash to OpenStruct (#81) > >Hi > >Another idea would be to use the latest CVS version of RbYAML to do it >like this, where data is a string or IO to the YAML data: > >require 'ostruct' >require 'rbyaml' > >RbYAML.add_builtin_ctor("map") {|ctor,node| > OpenStruct.new(ctor.construct_mapping(node)) >} > >RbYAML.load(data) > >This dosn't really work for mentalguys problem, since RbYAML doesn't >support recursive nodes right now. > >Regards > Ola Bini Correction, CVS RbYAML now handles recursive structures enough to handle mentalguys problem too. Another thing that will not work correctly, though, is this map: --- x: 1 y: 2 z: 3 because the 'y' will be translated to a boolean true, per the YAML spec, but OpenStruct tries to call to_sym on the keys, which boolean doesn't handle. The easiest fix is to class TrueClass; def to_sym; :true end; end and class FalseClass; def to_sym; :false end; end but then this will actually become a struct that looks like this: # which isn't totally obvious. /O