# Array to Hash

**URL:** <https://rubytalk.org/t/array-to-hash/11192>\
**Category:** ruby-talk\
**Created:** [24 April 2004 09:54 UTC](https://rubytalk.org/t/array-to-hash/11192 "2004-04-24T09:54:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [24 April 2004 09:54 UTC](https://rubytalk.org/t/array-to-hash/11192/1 "2004-04-24T09:54:11Z")

</div>

> There’s one reason why this may not be a standard method:  
> there are at least two equally reasonable representations of  
> a mapping stored in an array:
> 
> Nested arrays as returned by Hash#to\_a: [[key1, val1], [key2, val2]]
> 
> A sequence: [key1, val1, key2, val2] as used by Hash
> 
> Obviously there is no simple way from a Hash to an Array and  
> back to a Hash.  
> (Btw: is that ever needed? I mean, if you have a hash you can  
> easily create a copy without an intermediate array.)
> 
> So the question is wich representation Enumerable#to\_h should  
> expect - nested or sequence?

I am concerned more on the language.

Which would you prefer?

Hash#to\_a or Hash[\*mumblearray.flatten]

IMHO, I prefer the first; more rubyish. The second looks weird, and my brain  
aches just to remember it.

Now, since the first notation returns an array of pairs, then _it_ is in  
consonance with the hash notation (since hashes are pairs too). Ergo, to  
answer your question, I choose the nested.

Ok, now I have a question for you robert (since you’re one of the ruby gurus  
here), Is it possible for #to\_h to determine if the array is nested or not,  
and then adjust accordingly?

> ```
> robert
> 
> ```

kind regards -botp

> **···**
>
> Robert Klemme [[mailto:bob.news@gmx.net](mailto:bob.news@gmx.net)] wrote:

---

<div class="post-metadata">

**Author:** ![Mark\_Hubbart](https://avatars.discourse-cdn.com/v4/letter/m/b77776/32.png) [@Mark\_Hubbart](https://rubytalk.org/u/Mark_Hubbart)\
**Post date:** [24 April 2004 10:22 UTC](https://rubytalk.org/t/array-to-hash/11192/2 "2004-04-24T10:22:08Z")

</div>

> > There’s one reason why this may not be a standard method:  
> > there are at least two equally reasonable representations of  
> > a mapping stored in an array:
> > 
> > Nested arrays as returned by Hash#to\_a: [[key1, val1], [key2, val2]]
> > 
> > A sequence: [key1, val1, key2, val2] as used by Hash
> > 
> > Obviously there is no simple way from a Hash to an Array and  
> > back to a Hash.  
> > (Btw: is that ever needed? I mean, if you have a hash you can  
> > easily create a copy without an intermediate array.)
> > 
> > So the question is wich representation Enumerable#to\_h should  
> > expect - nested or sequence?
> 
> I am concerned more on the language.
> 
> Which would you prefer?
> 
> Hash#to\_a or Hash[\*mumblearray.flatten]
> 
> IMHO, I prefer the first; more rubyish. The second looks weird, and my  
> brain  
> aches just to remember it.
> 
> Now, since the first notation returns an array of pairs, then _it_ is  
> in  
> consonance with the hash notation (since hashes are pairs too). Ergo,  
> to  
> answer your question, I choose the nested.
> 
> Ok, now I have a question for you robert (since you’re one of the ruby  
> gurus  
> here), Is it possible for #to\_h to determine if the array is nested or  
> not,  
> and then adjust accordingly?

Well, I’m not robert 🙂 but here’s one way to check. If the array  
consists of objects that respond to #size and #inject, and #size  
returns 2, it is assumed that this is a nested array. Otherwise, it  
runs it through the Hash[obj] thing.

class Array  
def to\_h  
if all?{|e| e.respond\_to?(:size, :inject) and e.size == 2}  
inject({}){|h,(k,v)| h[k] = v; h}  
else  
Hash[\*self]  
end  
end  
end

–Mark

> **···**
>
> On Apr 24, 2004, at 2:54 AM, Peña, Botp wrote:
> 
> > Robert Klemme [[mailto:bob.news@gmx.net](mailto:bob.news@gmx.net)] wrote:
> 
> > > ```
> > > robert
> > > 
> > > ```
> > 
> > kind regards -botp
