# Hash to array of arrays

**URL:** <https://www.ruby-forum.com/t/hash-to-array-of-arrays/90918>\
**Category:** Rails\
**Created:** [April 3, 2007, 12:07am UTC](https://www.ruby-forum.com/t/hash-to-array-of-arrays/90918 "2007-04-03T00:07:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [April 3, 2007, 12:07am UTC](https://www.ruby-forum.com/t/hash-to-array-of-arrays/90918/1 "2007-04-03T00:07:04Z")

</div>

Hi,

I need to convert this data I pulled from a database:

[#\<Metric:0x24fbbec @attributes={“interested”=\>“3”, “prequalified”=\>“3”,  
“office”=\>“Denver, CO”, “passed”=\>“1”, “qualified”=\>“3”}\>,  
#\<Metric:0x24fbb10 @attributes={“interested”=\>“1”, “prequalified”=\>“1”,  
“office”=\>“McLean, VI”, “passed”=\>“1”, “qualified”=\>“1”}\>,  
#\<Metric:0x24fbae8 @attributes={“interested”=\>“1”, “prequalified”=\>“1”,  
“office”=\>“Raleigh, NC”, “passed”=\>“1”, “qualified”=\>“1”}\>,  
#\<Metric:0x24fbac0 @attributes={“interested”=\>“1”, “prequalified”=\>“1”,  
“office”=\>“Troy, MI”, “passed”=\>“0”, “qualified”=\>“1”}\>]

to this format:

[[“3”, "3’, “Denver, CO”, “1”, “3”], [“1”, “1”, “McLean, VI”, “1”, “1”],  
[“1”, “1”, “Raleigh, NC”, “1”, “1”], [“1”, “1”, “Troy, MI”, “0”, “1”]]

I’m a little stuck on how to do this.

Any ideas?

Thanks,

David

---

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [April 3, 2007, 12:32am UTC](https://www.ruby-forum.com/t/hash-to-array-of-arrays/90918/2 "2007-04-03T00:32:41Z")

</div>

The interesting part of this is that you have an array of objects that  
contain a hash! So the question is: how do you extract the hash out of  
every object and convert it to an array?

Hash to array -\> Hash#values  
Collect the results -\> Array#collect

result = @metrics.collect do |metric|  
metric.attributes.values  
end

The ultra-nice solution would be to write an encoder (like the to\_json  
stuff) for this, which is left to the reader as an exercise - but this  
one does the job good enough.

---

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [April 3, 2007, 5:29pm UTC](https://www.ruby-forum.com/t/hash-to-array-of-arrays/90918/3 "2007-04-03T17:29:45Z")

</div>

That works well.

Ideally, I want to get these values into xml format like:

 Denver, CO 3 3 1 3 . . .

I know who to do it when I get a single array of two values, but I’m not  
sure how to iterate over an array of arrays containing multiple values.

Any ideas?

---

<div class="post-metadata">

**Author:** ![drlelon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ee59a6/32.png) [@drlelon](https://www.ruby-forum.com/u/drlelon)\
**Post date:** [April 3, 2007, 6:00pm UTC](https://www.ruby-forum.com/t/hash-to-array-of-arrays/90918/4 "2007-04-03T18:00:10Z")

</div>

Aaah!  
First, try if this:

@metrics.to\_xml (it can be called on the whole collection)

fits your needs.

(docs at  
[http://api.rubyonrails.org/classes/ActiveRecord/XmlSerialization.html](http://api.rubyonrails.org/classes/ActiveRecord/XmlSerialization.html) )

If you want total costumization, i suggest to implement the  
to\_xml-Method for each model by yourself. There is an info on how to do  
this in the doc-page above.
