Bug #2711
closedinvalid yaml emitted for Struct
Description
=begin
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.
This causes rubyspec failure.
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 <top (required)>' /home/mame/work/ruby/spec/rubyspec/library/yaml/to_yaml_spec.rb:5:in
<top (required)>'
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?
Thanks,
--
Yusuke ENDOH [email protected]
=end
Updated by Anonymous over 15 years ago
- Status changed from Open to Closed
- % Done changed from 0 to 100
=begin
This issue was solved with changeset r26576.
Yusuke, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.
=end