Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Array to_xml fix

#1126

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}

Reported by Rudolf Gavlas · September 27th, 2008 @ 07:10 PM

State: resolved
Milestone: 2.x
Assigned to: DHH DHH
Importance: none

Activity

  1. Repository
    Repository
    • State changed from new to resolved

    (from [e466ae13e9b6d901e3e39bb9ceeafd83e266ce81]) Fixed the option merging in Array#to_xml [#1126 Array to_xml fix state:resolved] http://github.com/rails/rails/co...

    October 31st, 2008 @ 05:42 PM