1

I have to create XML that looks something like this:

<?xml version="1.0" ?>
<FirstLevel>
  <Package>
    <Name></Name>
  </Package>
  <Package>
    <Name></Name>
  </Package>
  ...
</FirstLevel>

As you can see, Package shows up multiple times at the same level in the structure.

I know you can't have duplicate keys in a Ruby hash, so I don't know how I would be able to go from a hash to XML when there are duplicate keys. Does anyone have any ideas?

I'm using Hash#to_xml to convert my hash to XML (made available by ActiveSupport I believe).

By the way, I'm using Rails.

1
  • Also you can use nokogiri xml to use/access the xml. Commented Jan 10, 2014 at 16:59

1 Answer 1

3

Okay I believe I figured it out. You have to use Hash#compare_by_identity. I believe this makes it so that the key lookups are done using object id as opposed to string matches.

I found it in "Ruby Hash with duplicate keys?".

{}.compare_by_identity

    h1 = {}
    h1.compare_by_identity
    h1["a"] = 1
    h1["a"] = 2
    p h1 # => {"a"=>1, "a"=>2}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.