From: Aaron Patterson Date: 2010-02-05T02:12:47+09:00 Subject: [ruby-core:28056] Re: [Bug:trunk] invalid yaml emitted for Struct On Thu, Feb 04, 2010 at 09:01:12PM +0900, Yusuke ENDOH wrote: > Hi, Aaron Patterson -- > > lib/yaml seems to emit invalid yaml document for Struct: > > > $ ./ruby -ryaml -e ' > Person = Struct.new(:name, :gender) > puts Person.new("Jane", "female").to_yaml > ' > --- !ruby/struct:Person > :name: Jane > :gender: female > > > The colon immediately before key names should not be emitted, I think. I believe this is actually valid YAML, but the behavior in 1.9 is not compatible with 1.8. > This causes rubyspec failure. > > > 1) > Object#to_yaml returns the YAML representation of a Struct object FAILED > Expected "--- !ruby/struct:Person \n:name: Jane\n:gender: female\n" > to match "--- !ruby/struct:Person\nname: Jane\ngender: female\n" > /home/mame/work/ruby/spec/rubyspec/library/yaml/to_yaml_spec.rb:58:in > `block (2 levels) in ' > /home/mame/work/ruby/spec/rubyspec/library/yaml/to_yaml_spec.rb:5:in > `' > > > This seems because the type of each element of Struct#members are changed > from String to Symbol on 1.9. In fact, the following patch works. > > > diff --git a/lib/yaml/rubytypes.rb b/lib/yaml/rubytypes.rb > index e8c0c89..71f911a 100644 > --- a/lib/yaml/rubytypes.rb > +++ b/lib/yaml/rubytypes.rb > @@ -72,7 +72,7 @@ class Struct > # > st = YAML::object_maker( struct_type, {} ) > st.members.each do |m| > - st.send( "#{m}=", val[m] ) > + st.send( "#{m}=", val[m.to_s] ) > end > props.each do |k,v| > st.instance_variable_set(k, v) > @@ -89,7 +89,7 @@ class Struct > # > out.map( taguri, to_yaml_style ) do |map| > self.members.each do |m| > - map.add( m, self[m] ) > + map.add( m.to_s, self[m.to_s] ) > end > self.to_yaml_properties.each do |m| > map.add( m, instance_variable_get( m ) ) > > > Could you commit this? I've committed it and fixed the tests. Thanks for the patch! -- Aaron Patterson http://tenderlovemaking.com/