This project is archived and is in readonly mode.

#1126 ✓resolved
Rudolf Gavlas

Array to_xml fix

Reported by Rudolf Gavlas | September 27th, 2008 @ 07:10 PM | in 2.x

Short description:

See the patch, the hash passed as argument to recursing to_xml method is pointing to the same structure as the original opts hash.

$ rails -v Rails 2.0.2 $ ruby -v ruby 1.8.6 (2007-03-13 patchlevel 0) [i386-netbsdelf]

(rails console) before the patch:

>> print [[], []].to_xml <?xml version="1.0" encoding="UTF-8"?> <arrays type="array"> <array type="array"/> <nil-classes type="array"/> </arrays> => nil

after the patch:

>> print [[],[]].to_xml <?xml version="1.0" encoding="UTF-8"?> <arrays type="array"> <array type="array"/> <array type="array"/> </arrays> => nil

Explanation:

After the merge! method on line:

each { |e| e.to_xml(opts.merge({ :skip_instruct => true })) }

the hash passed to next iteration of to_xml method is pointing to the same structure as the forcibly merged opts hash. [1] Then after:

root = options.delete(:root).to_s

this parameter is deleted from the original opts hash too and to the next iteration are going wrong opts.

As there is no need to use merge! here because simple merge returns the same, and we repeat the merging in each iteration (no need to forcibly change the original hash), I propose this patch.

[1] It is the same as in this example: $ irb irb(main):001:0> a = {'a' => 1, 'b' => 2} => {"a"=>1, "b"=>2} irb(main):002:0> b = {'b' => 3, 'c' => 4} => {"b"=>3, "c"=>4} irb(main):003:0> c = a.merge!(b) => {"a"=>1, "b"=>3, "c"=>4} irb(main):004:0> a => {"a"=>1, "b"=>3, "c"=>4} irb(main):005:0> c => {"a"=>1, "b"=>3, "c"=>4} irb(main):006:0> c.delete('c') => 4 irb(main):007:0> a => {"a"=>1, "b"=>3} irb(main):008:0> c => {"a"=>1, "b"=>3}

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Attachments

Referenced by

Pages