# Array to Hash

**URL:** <https://rubytalk.org/t/array-to-hash/11189>\
**Category:** ruby-talk\
**Created:** [24 April 2004 04:05 UTC](https://rubytalk.org/t/array-to-hash/11189 "2004-04-24T04:05:20Z")\
**Posts on this page:** 15\
**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 04:05 UTC](https://rubytalk.org/t/array-to-hash/11189/1 "2004-04-24T04:05:20Z")

</div>

Hi Ruby friends:

conversion fr hash to array is fine.

> > h  
> > =\> {“x”=\>1, “y”=\>3}  
> > a = h.to\_a  
> > =\> [[“x”, 1], [“y”, 3]]

no brainer there for a nuby like me 🙂

but…

conversion fr array to hash… is quite a journey…

> > h2 = a.to\_h  
> > NoMethodError: undefined method `to\_h’ for [[“x”, 1], [“y”, 3]]:Array

> > h2 = a.to\_hash  
> > NoMethodError: undefined method `to\_hash’ for [[“x”, 1], [“y”, 3]]:Array

Finally, I got it (dug the archives)…

> > h2 = Hash[\*a]  
> > =\> {[“x”, 1]=\>[“y”, 3]}

But, it looks weird. No, not just the cryptic Hash\*a, but the result as  
well. I do not see any use of the result.

I would rather prefer:

> > h2 = a.to\_hash # or a.to\_h  
> > {“x”=\>1, “y”=\>3}

Seems no brainer if that’s the case.  
Is there any reason why not?

kind regards -botp

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [24 April 2004 04:21 UTC](https://rubytalk.org/t/array-to-hash/11189/2 "2004-04-24T04:21:24Z")

</div>

Peña, Botp wrote:

> Finally, I got it (dug the archives)…
> 
> > > h2 = Hash[\*a]
> 
> =\> {[“x”, 1]=\>[“y”, 3]}
> 
> But, it looks weird. No, not just the cryptic Hash\*a, but the result as  
> well. I do not see any use of the result.
> 
> I would rather prefer:
> 
> > > h2 = a.to\_hash # or a.to\_h
> 
> {“x”=\>1, “y”=\>3}
> 
> Seems no brainer if that’s the case.  
> Is there any reason why not?

Well, legacy code expects a flat array of pairs… so Hash(\*(a.flatten))  
should work for you. But changing the way that works could break others’  
code.

Yet it does seem odd that Hash.to\_a doesn’t return a form that Hash[\*a]  
can use as-is.

Hal

---

<div class="post-metadata">

**Author:** ![WATANABE\_Hirofumi1](https://avatars.discourse-cdn.com/v4/letter/w/c2a13f/32.png) [@WATANABE\_Hirofumi1](https://rubytalk.org/u/WATANABE_Hirofumi1)\
**Post date:** [24 April 2004 04:22 UTC](https://rubytalk.org/t/array-to-hash/11189/3 "2004-04-24T04:22:40Z")

</div>

Hi,

“Peña, Botp” [botp@delmonte-phil.com](mailto:botp@delmonte-phil.com) writes:

> Finally, I got it (dug the archives)…
> 
> > > h2 = Hash[\*a]  
> > > =\> {[“x”, 1]=\>[“y”, 3]}
> 
> But, it looks weird. No, not just the cryptic Hash\*a, but the result as  
> well. I do not see any use of the result.
> 
> I would rather prefer:
> 
> > > h2 = a.to\_hash # or a.to\_h  
> > > {“x”=\>1, “y”=\>3}

Array#flatten

> > h2 = Hash[\*a.flatten]  
> > =\> {“x”=\>1, “y”=\>3}

> **···**
>
> –  
> eban

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [24 April 2004 04:36 UTC](https://rubytalk.org/t/array-to-hash/11189/4 "2004-04-24T04:36:48Z")

</div>

Hi,

At Sat, 24 Apr 2004 13:05:20 +0900,  
Peña, Botp wrote in [ruby-talk:98203]:

> I would rather prefer:
> 
> > > h2 = a.to\_hash # or a.to\_h  
> > > {“x”=\>1, “y”=\>3}

module Enumerable  
def to\_h  
inject({}) {|h, (k, v)| h[k] = v; h}  
end  
end

> **···**
>
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [24 April 2004 12:54 UTC](https://rubytalk.org/t/array-to-hash/11189/5 "2004-04-24T12:54:06Z")

</div>

Hi –

“Peña, Botp” [botp@delmonte-phil.com](mailto:botp@delmonte-phil.com) writes:

> conversion fr hash to array is fine.
> 
> > > h  
> > > =\> {“x”=\>1, “y”=\>3}  
> > > a = h.to\_a  
> > > =\> [[“x”, 1], [“y”, 3]]
> 
> no brainer there for a nuby like me 🙂

I think there’s a time limit on describing oneself as a nuby 🙂

> but…
> 
> conversion fr array to hash… is quite a journey…  
> […]  
> Finally, I got it (dug the archives)…
> 
> > > h2 = Hash[\*a]  
> > > =\> {[“x”, 1]=\>[“y”, 3]}

Dig the archives a little more 🙂 There’s been a lot of discussion of  
this in the past, including a rejected RCR  
([RCRS](http://www.rcrchive.net/rgarchive/rejected.html#rcr12), posted under  
‘dave’ due to some glitch but actually submitted by me). I called it  
“hashify”, but as you’ll see from the many ruby-talk references it’s  
really the to\_h[ash] idea.

David

> **···**
>
> –  
> David A. Black  
> [dblack@wobblini.net](mailto:dblack@wobblini.net)

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard](https://avatars.discourse-cdn.com/v4/letter/a/a8b319/32.png) [@Ara.T.Howard](https://rubytalk.org/u/Ara.T.Howard)\
**Post date:** [24 April 2004 05:14 UTC](https://rubytalk.org/t/array-to-hash/11189/6 "2004-04-24T05:14:05Z")

</div>

> Hi,
> 
> At Sat, 24 Apr 2004 13:05:20 +0900,  
> Peña, Botp wrote in [ruby-talk:98203]:
> 
> > I would rather prefer:
> > 
> > > > h2 = a.to\_hash # or a.to\_h  
> > > > {“x”=\>1, “y”=\>3}
> 
> module Enumerable  
> def to\_h  
> inject({}) {|h, (k, v)| h[k] = v; h}  
> ^^^^^^  
> ^^^^^^  
> ^^^^^^  
> what’s this?  
> end  
> end

-a

> **···**
>
> ## On Sat, 24 Apr 2004 [nobu.nokada@softhome.net](mailto:nobu.nokada@softhome.net) wrote:
> 
> ===============================================================================
> 
> > EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov  
> > PHONE :: 303.497.6469  
> > ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328  
> > URL :: [Solar-Terrestrial Physics Data | NCEI](http://www.ngdc.noaa.gov/stp/)  
> > TRY :: for l in ruby perl;do $l -e “print "\x3a\x2d\x29\x0a"”;done  
> > ===============================================================================

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [24 April 2004 05:49 UTC](https://rubytalk.org/t/array-to-hash/11189/7 "2004-04-24T05:49:09Z")

</div>

Ara.T.Howard wrote:

> > module Enumerable  
> > def to\_h  
> > inject({}) {|h, (k, v)| h[k] = v; h}
> 
> ```
> ^^^^^^
> ^^^^^^
> ^^^^^^
> what's this?
> 
> ```

inject will get yielded the current state of the result Hash and a new  
[key, value] Array. The () syntax lets you access the components of that  
Array directly through the variables you use.

This is similar to this assignment:

pair, (key, value) = [“one”, 1], [“two”, 2]  
pair # =\> [“one”, 1]  
key # =\> “two”  
value # =\> 2

This is also explained in the Pickaxe book:  
[http://www.rubycentral.com/book/tut\_expressions.html#UD](http://www.rubycentral.com/book/tut_expressions.html#UD)

Regards,  
Florian Gross

> **···**
>
> > On Sat, 24 Apr 2004 [nobu.nokada@softhome.net](mailto:nobu.nokada@softhome.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 05:57 UTC](https://rubytalk.org/t/array-to-hash/11189/8 "2004-04-24T05:57:09Z")

</div>

module Enumerable  
def to\_h  
inject({}) {|h, (k, v)| h[k] = v; h}  
^ ^^^^  
1 2  
1: is the hash that keeps being passed through inject each time.  
2: is the key=\>value pair, expanding it from a two element array.

I didn’t even realize you could do that type of assignment in a block’s  
parameter list, but hey! that’s pretty cool.

I think this should be a standard Array method. There have been many  
times I’ve done just this type of thing to get a hash from an array,  
for various reasons.

cheers,  
–Mark

> **···**
>
> On Apr 23, 2004, at 10:14 PM, Ara.T.Howard wrote:
> 
> > On Sat, 24 Apr 2004 [nobu.nokada@softhome.net](mailto:nobu.nokada@softhome.net) wrote:
> > 
> > > Hi,
> > > 
> > > At Sat, 24 Apr 2004 13:05:20 +0900,  
> > > Peña, Botp wrote in [ruby-talk:98203]:
> > > 
> > > > I would rather prefer:
> > > > 
> > > > > > h2 = a.to\_hash # or a.to\_h  
> > > > > > {“x”=\>1, “y”=\>3}
> > > 
> > > module Enumerable  
> > > def to\_h  
> > > inject({}) {|h, (k, v)| h[k] = v; h}  
> > > ^^^^^^  
> > > ^^^^^^  
> > > ^^^^^^  
> > > what’s this?  
> > > end  
> > > end

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@Nobuyoshi\_Nakada](https://rubytalk.org/u/Nobuyoshi_Nakada)\
**Post date:** [24 April 2004 06:04 UTC](https://rubytalk.org/t/array-to-hash/11189/9 "2004-04-24T06:04:12Z")

</div>

Hi,

At Sat, 24 Apr 2004 14:57:09 +0900,  
Mark Hubbart wrote in [ruby-talk:98217]:

> I didn’t even realize you could do that type of assignment in a block’s  
> parameter list, but hey! that’s pretty cool.

Block parameter list is equivalent to multiple assignment now,  
so you can create new variables there.

> **···**
>
> –  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [24 April 2004 09:24 UTC](https://rubytalk.org/t/array-to-hash/11189/10 "2004-04-24T09:24:06Z")

</div>

“Mark Hubbart” [discord@mac.com](mailto:discord@mac.com) schrieb im Newsbeitrag  
[news:36BA84AE-95B4-11D8-9D1D-000502FDD5CC@mac.com](mailto:news:36BA84AE-95B4-11D8-9D1D-000502FDD5CC@mac.com)…

> > > Hi,
> > > 
> > > At Sat, 24 Apr 2004 13:05:20 +0900,  
> > > Peña, Botp wrote in [ruby-talk:98203]:
> > > 
> > > > I would rather prefer:
> > > > 
> > > > > > h2 = a.to\_hash # or a.to\_h  
> > > > > > {“x”=\>1, “y”=\>3}
> > > 
> > > module Enumerable  
> > > def to\_h  
> > > inject({}) {|h, (k, v)| h[k] = v; h}  
> > > ^^^^^^  
> > > ^^^^^^  
> > > ^^^^^^  
> > > what’s this?  
> > > end  
> > > end
> 
> module Enumerable  
> def to\_h  
> inject({}) {|h, (k, v)| h[k] = v; h}  
> ^ ^^^^  
> 1 2  
> 1: is the hash that keeps being passed through inject each time.  
> 2: is the key=\>value pair, expanding it from a two element array.
> 
> I didn’t even realize you could do that type of assignment in a block’s  
> parameter list, but hey! that’s pretty cool.
> 
> I think this should be a standard Array method. There have been many  
> times I’ve done just this type of thing to get a hash from an array,  
> for various reasons.

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?

```
robert

```

> **···**
>
> > On Apr 23, 2004, at 10:14 PM, Ara.T.Howard wrote:
> > 
> > > On Sat, 24 Apr 2004 [nobu.nokada@softhome.net](mailto:nobu.nokada@softhome.net) wrote:

---

<div class="post-metadata">

**Author:** ![Gergely\_Kontra](https://avatars.discourse-cdn.com/v4/letter/g/7c8e57/32.png) [@Gergely\_Kontra](https://rubytalk.org/u/Gergely_Kontra)\
**Post date:** [28 April 2004 16:12 UTC](https://rubytalk.org/t/array-to-hash/11189/11 "2004-04-28T16:12:08Z")

</div>

Why _cannot_ I simply say?  
pair, [key, value] = ["one,1],[“two”,2]  
 is for arrays, () is for tuples.

Gergo

> **···**
>
> On 0424, Florian Gross wrote:
> 
> > pair, (key, value) = [“one”, 1], [“two”, 2]  
> > pair # =\> [“one”, 1]  
> > key # =\> “two”  
> > value # =\> 2
> 
> –  
> ±[Kontra, Gergely[kgergely@mcl.hu](mailto:kgergely@mcl.hu) PhD student Room IB113 ]---------+
> 
> > [http://www.mcl.hu/~kgergely](http://www.mcl.hu/~kgergely) “Olyan langesz vagyok, hogy |  
> > Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom” |  
> > ±- Magyar php mirror es magyar php dokumentacio: [http://hu.php.net](http://hu.php.net) --+

---

<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 09:39 UTC](https://rubytalk.org/t/array-to-hash/11189/12 "2004-04-24T09:39:44Z")

</div>

It seems to me that it should be the one that Hash#to\_a generates:  
nested pairs. They’re already almost a hash anyway; Array has a few  
methods for that “format” that allow you to lookup items as if it were  
a hash. Why not be able to convert to and from? Makes sense to me.

cheers,  
–Mark

> **···**
>
> On Apr 24, 2004, at 2:24 AM, Robert Klemme wrote:
> 
> > So the question is wich representation Enumerable#to\_h should expect -  
> > nested or sequence?

---

<div class="post-metadata">

**Author:** ![Jean-Hugues\_ROBERT](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@Jean-Hugues\_ROBERT](https://rubytalk.org/u/Jean-Hugues_ROBERT)\
**Post date:** [24 April 2004 13:04 UTC](https://rubytalk.org/t/array-to-hash/11189/13 "2004-04-24T13:04:09Z")

</div>

I believe coercion should be as loose less as possible. As a result it  
is difficult to imagine that a generic solution is optimal. Only the  
actual class knows the optimum. Yet…

One may argue that an Array is a degenerated Hash where the keys are  
integer indices. i.e.  
[“a”,“b”].to\_h() == { 0 =\> “a”, 1 =\> “b” }

One may argue that the loose less convertion of a Hash to an Array is  
an an array of [key, value]. i.e.  
{ 0 =\> “a”, 1 =\> “b”}.to\_a() == [[0,“a”],[1,“b”]]  
(Ruby does that today)

hum… issue is a.to\_h().to\_a() is quite different from original a.

Better:

# What you get when converting a Hash into an Array, see Hash##to\_a()

class HashAsArray \< Array  
def to\_h()  
# Assume self is an array of [key, value], raise exception if not true  
# Error checking code skipped, ToDo: it  
Hash[\*flatten()]  
end  
end

# What you get when converting an Array into a Hash, see Array##to\_h()

class ArrayAsHash \< Hash  
def to\_a()  
# Assume keys are integers indices, raise exception if not true  
# Error checking code skipped, ToDo: it  
super().sort { |(ka,va),(kb,vb)| ka \<=\> kb }.collect { |(k,v)| v }  
# ToDo: There must be a faster solution to do that.  
end  
end

# Now, have Hash.to\_a() return an HashAsArray instead of an Array,

# and, have Array.to\_h() return an ArrayAsHash.

class Hash  
alias ruby\_to\_a to\_a  
def to\_a()  
HashAsArray.new().replace( ruby\_to\_a())  
end  
def to\_h()  
self  
end  
end

class Array  
def to\_h()  
ii = 0  
aa =   
self.each { |v| aa \<\< (ii += 1); aa \<\< v }  
return ArrayAsHash[\*aa]  
end  
end

# As a result, an\_array.to\_h().to\_a() is OK and

# a\_hash.to\_a().to\_h() is ok too.

p a = [“a”, “b”, “c”, “d”, “e”, “f”]  
p h = a.to\_h()  
p h.class()  
p h.kind\_of?( Hash)  
p h.class() == Hash # not true anymore… but:  
case h  
when Hash then p “OK”  
end  
p aa = h.to\_a()  
p aa.class()  
p a.to\_h().to\_a() == a  
p a.to\_a().to\_h().to\_a() == a

p h = { “key\_a” =\> “a”, “key\_b” =\> “b” }  
p a = h.to\_a()  
p a.class()  
p a.kind\_of?( Array)  
p a.class() == Array # not true anymore… but:  
case a  
when Array then p “OK”  
end  
p hh = a.to\_h()  
p hh.class()  
p h.to\_a().to\_h() == h  
p h.to\_h().to\_a().to\_h() == h

# Most visible change is about these beeing false:

p Array == HashAsArray  
p HashAsArray == Array  
p Hash == ArrayAsHash  
p ArrayAsHash == Hash

# Workaround is so ugly that I don’t present it here.

# I believe this only change cannot break much existing code,

# but very unprobable code that would depend on Hash##to\_a()

# beeing of class Array precisely. Questionnable

# code that does not respect the OO idea of is\_a relationship

# and test class for equality using == instead of

# kind\_of? or === (in case statements).

Any drawbacks (beside minor one about .class() == changed) ?

Jean-Hugues

> **···**
>
> At 18:24 24/04/2004 +0900, you wrote:
> 
> > So the question is wich representation Enumerable#to\_h should expect -  
> > nested or sequence?  
> > robert

---

<div class="post-metadata">

**Author:** ![Jean-Hugues\_ROBERT](https://avatars.discourse-cdn.com/v4/letter/j/e47c2d/32.png) [@Jean-Hugues\_ROBERT](https://rubytalk.org/u/Jean-Hugues_ROBERT)\
**Post date:** [28 April 2004 19:30 UTC](https://rubytalk.org/t/array-to-hash/11189/14 "2004-04-28T19:30:29Z")

</div>

> > pair, (key, value) = [“one”, 1], [“two”, 2]  
> > pair # =\> [“one”, 1]  
> > key # =\> “two”  
> > value # =\> 2
> 
> Why _cannot_ I simply say?  
> pair, [key, value] = ["one,1],[“two”,2]  
> is for arrays, () is for tuples.

Why, yeah, Why ?

In the context of a lvalue (something at the left of  
the assignment operator), you must provide a lvalue,  
instead of a value.

It is easy to build values, included nested one using  
. But only the compiler can build a lvalue.

Probably for historical reasons, the compiler wants  
parenthesis within lvalue. And infer the missing  
outer ones when it encounter a , before =.

I suppose that the reason is related to the syntax  
for formal argument description:  
def meth( a, b, c)  
end

As a result  
def meth( a, [b1,b2], c) looks unbalanced compared to  
def meth( a, (b1,b2), c) that Ruby requires.

I guess this is why the later syntax was picked, and  
propagated up to the = assignment operator.

Note: That syntax applies for Proc’s parameters too.

I suppose that there is some true value in that syntax,  
because, within (), access to variables is by reference,  
not by value.

All of this is the result of evolution.  
For sure, something like a, = meth() is not as obvious  
as (a) = meth() or, better, [a] = meth().

But that’s the way it is, I suppose you have to accept  
it.

Yours,

Jean-Hugues

> **···**
>
> At 01:12 29/04/2004 +0900, you wrote:
> 
> > On 0424, Florian Gross wrote:
> 
> > Gergo
> > 
> > –  
> > ±[Kontra, Gergely[kgergely@mcl.hu](mailto:kgergely@mcl.hu) PhD student Room IB113 ]---------+
> > 
> > > [http://www.mcl.hu/~kgergely](http://www.mcl.hu/~kgergely) “Olyan langesz vagyok, hogy |  
> > > Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom” |  
> > > ±- Magyar php mirror es magyar php dokumentacio: [http://hu.php.net](http://hu.php.net) --+
> 
> * * *
> 
> Web: [http://hdl.handle.net/1030.37/1.1](http://hdl.handle.net/1030.37/1.1)  
> Phone: +33 (0) 4 92 27 74 17

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [26 April 2004 08:44 UTC](https://rubytalk.org/t/array-to-hash/11189/15 "2004-04-26T08:44:11Z")

</div>

“Mark Hubbart” [discord@mac.com](mailto:discord@mac.com) schrieb im Newsbeitrag  
[news:4DF38309-95D3-11D8-9D1D-000502FDD5CC@mac.com](mailto:news:4DF38309-95D3-11D8-9D1D-000502FDD5CC@mac.com)…

> > So the question is wich representation Enumerable#to\_h should expect -  
> > nested or sequence?
> 
> It seems to me that it should be the one that Hash#to\_a generates:  
> nested pairs. They’re already almost a hash anyway; Array has a few  
> methods for that “format” that allow you to lookup items as if it were  
> a hash. Why not be able to convert to and from? Makes sense to me.

If it is a Hash then why don’t you have the data in a hash in the first  
place? I mean, the data should not reside in an array if they are to be  
used hash-like.

Dunno whether this kind of conversion occurs often. I for my part didn’t  
need that till today. Just my 0.02…

```
robert

```

> **···**
>
> > On Apr 24, 2004, at 2:24 AM, Robert Klemme wrote:
