# Array#to\_h

**URL:** <https://rubytalk.org/t/array-to-h/51465>\
**Category:** ruby-talk\
**Created:** [31 January 2009 19:49 UTC](https://rubytalk.org/t/array-to-h/51465 "2009-01-31T19:49:27Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [31 January 2009 19:49 UTC](https://rubytalk.org/t/array-to-h/51465/1 "2009-01-31T19:49:27Z")

</div>

Not that I would find it useful at all, but is there is a Hash#to\_a  
should there not be an Array#to\_h?  
Thanks!  
-=r

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [31 January 2009 20:03 UTC](https://rubytalk.org/t/array-to-h/51465/2 "2009-01-31T20:03:44Z")

</div>

Hi --

> Not that I would find it useful at all, but is there is a Hash#to\_a  
> should there not be an Array#to\_h?

Not if it's not useful 🙂 It might be, though. It's been talked about  
a lot over the years. As I recall, part of the problem is the question  
of what it would mean; for example, given this:

&nbsp;&nbsp;&nbsp;["a","b","c","d"].to\_h

is it

&nbsp;&nbsp;&nbsp;["a" =\> "b", "c" =\> "d"]

or

&nbsp;&nbsp;&nbsp;[0 =\> "a", 1 =\> "b", ....]

?

David

> **···**
>
> On Sun, 1 Feb 2009, Roger Pack wrote:
> 
> --  
> David A. Black / Ruby Power and Light, LLC  
> Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
> Coming in 2009: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))
> 
> [http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [31 January 2009 20:04 UTC](https://rubytalk.org/t/array-to-h/51465/3 "2009-01-31T20:04:32Z")

</div>

> Not that I would find it useful at all, but is there is a Hash#to\_a  
> should there not be an Array#to\_h?  
> Thanks!

A possible implementation:

> > class Array  
> > def to\_h  
> > Hash[\*self]  
> > end  
> > end

=\> nil

> > [1,2,3,4].to\_h

=\> {1=\>2, 3=\>4}

Jesus.

> **···**
>
> On Sat, Jan 31, 2009 at 8:49 PM, Roger Pack \<rogerpack2005@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Joshua\_Ballanco](https://avatars.discourse-cdn.com/v4/letter/j/d07c76/32.png) [@Joshua\_Ballanco](https://rubytalk.org/u/Joshua_Ballanco)\
**Post date:** [31 January 2009 20:09 UTC](https://rubytalk.org/t/array-to-h/51465/4 "2009-01-31T20:09:56Z")

</div>

See here:  
[http://www.fivesevensix.com/posts/2005/05/20/array-to\_h](http://www.fivesevensix.com/posts/2005/05/20/array-to_h)

...and here:  
[http://drawohara.com/post/70998078/ruby-array-to-hash-rocks](http://drawohara.com/post/70998078/ruby-array-to-hash-rocks)

- Josh

> **···**
>
> On Jan 31, 2009, at 2:49 PM, Roger Pack wrote:
> 
> > Not that I would find it useful at all, but is there is a Hash#to\_a  
> > should there not be an Array#to\_h?  
> > Thanks!  
> > -=r  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Sean\_O\_Halpin](https://avatars.discourse-cdn.com/v4/letter/s/ac91a4/32.png) [@Sean\_O\_Halpin](https://rubytalk.org/u/Sean_O_Halpin)\
**Post date:** [1 February 2009 17:31 UTC](https://rubytalk.org/t/array-to-h/51465/5 "2009-02-01T17:31:45Z")

</div>

It's useful in Ruby 1.8.x for those enumerable methods which return arrays, e..g

irb --\> {:a =\> 1, :b =\> 2, :c =\> 3}.select{|k, v| v % 2 == 1 }  
&nbsp;&nbsp;&nbsp;&nbsp;==\> [[:c, 3], [:a, 1]]

irb --\> {:a =\> 1, :b =\> 2, :c =\> 3}.select{|k, v| v % 2 == 1 }.to\_hash  
&nbsp;&nbsp;&nbsp;&nbsp;==\> {:a=\>1, :c=\>3}

whereas in Ruby 1.9.x, select (map, etc.) return hashes:

irb(main):001:0\> {:a =\> 1, :b =\> 2, :c =\> 3}.select{|k, v| v % 2 == 1 }  
=\> {:a=\>1, :c=\>3}

The problem is, as other posters have noted, that there is no 1-1  
mapping between an array and a hash. Having said that, I've found the  
following to be handy (mainly in restoring a hash after the 1.8.x  
transformation to an array):

module ToHash  
&nbsp;&nbsp;if RUBY\_VERSION =~ /^1.9/  
&nbsp;&nbsp;&nbsp;&nbsp;def to\_hash  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hash[\*flatten(1)]  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;def to\_hash  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hash[\*(inject() {|arr, i| i.kind\_of?(Array) ? arr.push(\*i) :  
arr.push(i) })]  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

used like this:

&nbsp;&nbsp;b = [  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[:a, [[1, 2], [3, 4]]],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[:b, 3],  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[:c, { :d =\> 4 }],  
&nbsp;&nbsp;&nbsp;&nbsp;]

&nbsp;&nbsp;p b.extend(ToHash).to\_hash

Regards,  
Sean

> **···**
>
> On Sat, Jan 31, 2009 at 7:49 PM, Roger Pack \<rogerpack2005@gmail.com\> wrote:
> 
> > Not that I would find it useful at all, but is there is a Hash#to\_a  
> > should there not be an Array#to\_h?  
> > Thanks!  
> > -=r

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [6 February 2009 14:12 UTC](https://rubytalk.org/t/array-to-h/51465/6 "2009-02-06T14:12:39Z")

</div>

The variety of possible good definitions make this hard to define. So  
it's understandable that is is not in Ruby core, though being able to  
convert back and forth between Hash#to\_a and Array#to\_h makes the most  
sense.

I was thinking about it some more and I was thinking about how the  
various options might be addressed. So I came up with this:

&nbsp;&nbsp;# Converts an associative array into a hash.

> **···**
>
> On Jan 31, 2:49 pm, Roger Pack \<rogerpack2...@gmail.com\> wrote:
> 
> > Not that I would find it useful at all, but is there is a Hash#to\_a  
> > should there not be an Array#to\_h?
> 
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;# a = [[:a,1], [:b,2] ]  
> &nbsp;&nbsp;# a.to\_h #=\> { :a=\>1, :b=\>2 }  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;# When a mixed or multi-element accociative array  
> &nbsp;&nbsp;# is used, the result is as follows:  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;# a = [[:a,1,2], [:b,2], [:c], :d ]  
> &nbsp;&nbsp;# a.to\_h #=\> { :a=\>[1,2], :b=\>2, :c=\>nil, :d=\>nil }  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;# If the fist entry of the subelements is the same, then  
> &nbsp;&nbsp;# the values will be merged using #concat.  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;# a = [[:a,1,2], [:a,3], [:a,4], [:a], :a ]  
> &nbsp;&nbsp;# a.to\_h #=\> { :a=\>[1,2,3,4,nil,nil] }  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;# The +mode+ can be set to effect the result. If it is  
> &nbsp;&nbsp;# set to +:array+ or +true+ then trailing arrays  
> &nbsp;&nbsp;# will be kept. Eg.  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;# a = [[:a,1,2], [:b,3], [:c] ]  
> &nbsp;&nbsp;# a.to\_h(true) #=\> { :a=\>[1,2], :b=\>[3], :c=\> }  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;# Setting the mode to +:splat+ will produce the same result  
> &nbsp;&nbsp;# as calling +Hash[\*array]+.  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;# a = [:a,1,:b,2,:c]  
> &nbsp;&nbsp;# a.to\_h:splat #=\> { :a=\>1, :b=\>2, :c=\>nil }  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;# Setting the mode to +:flat+ will produce the same result  
> &nbsp;&nbsp;# as calling +Hash[\*array.flatten]+.  
> &nbsp;&nbsp;#  
> &nbsp;&nbsp;# a = [:a,1,[:b,2,:c]]  
> &nbsp;&nbsp;# a.to\_h:flat #=\> { :a=\>1, :b=\>2, :c=\>nil }
> 
> &nbsp;&nbsp;def to\_h(mode=nil)  
> &nbsp;&nbsp;&nbsp;&nbsp;case mode  
> &nbsp;&nbsp;&nbsp;&nbsp;when :splat  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = dup  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a \<\< nil if a.size % 2 == 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hash[\*a]  
> &nbsp;&nbsp;&nbsp;&nbsp;when :flat  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a = flatten  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a \<\< nil if a.size % 2 == 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hash[\*a]  
> &nbsp;&nbsp;&nbsp;&nbsp;when :array, True  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h = {}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each do |k,\*v|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h[k] ||=   
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h[k].concat(v)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h  
> &nbsp;&nbsp;&nbsp;&nbsp;else  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h = {}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each do |k,\*v|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h[k] ||=   
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h[k].concat(v)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h.each do |k,v|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h[k] = v[0] if v.size \< 2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> By using a +mode+ we can offer a variety of common means of  
> conversion.
> 
> Of course, we could just make them all separate methods, ie. #to\_h,  
> #to\_h\_array, #to\_h\_splat, #to\_h\_flat. Maybe that is better? But then  
> that seems a bit more limiting, less dynamic, less open for new modes  
> or multiple labels for a single mode, and the method names look funny  
> (imo).
> 
> Thoughts?
> 
> T.

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [6 February 2009 17:39 UTC](https://rubytalk.org/t/array-to-h/51465/7 "2009-02-06T17:39:24Z")

</div>

irb(main):003:0\> Hash[\*[:foo,22, :bar,44] ]  
=\> {:foo=\>22, :bar=\>44}

> **···**
>
> On Jan 31, 1:49 pm, Roger Pack \<rogerpack2...@gmail.com\> wrote:
> 
> > Not that I would find it useful at all, but is there is a Hash#to\_a  
> > should there not be an Array#to\_h?  
> > Thanks!  
> > -=r  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![James\_Coglan2](https://avatars.discourse-cdn.com/v4/letter/j/c67d28/32.png) [@James\_Coglan2](https://rubytalk.org/u/James_Coglan2)\
**Post date:** [31 January 2009 20:47 UTC](https://rubytalk.org/t/array-to-h/51465/8 "2009-01-31T20:47:14Z")

</div>

This question came up a while back where I helped someone with yet another  
interpretation:

[http://blog.jcoglan.com/2008/09/08/enumerableto\_hash-for-unix-style-flags-in-ruby-methods/](http://blog.jcoglan.com/2008/09/08/enumerableto_hash-for-unix-style-flags-in-ruby-methods/)

The core problem with Hash#to\_a and any possible Enumerable#to\_h is it's not  
intuitively obvious what either should do, though Hash#to\_a is possibly  
slightly narrower in scope. There are any number of ways you might want to  
map one to the other, and that's really what map() and inject() are for.

> **···**
>
> 2009/1/31 Joshua Ballanco \<jballanc@gmail.com\>
> 
> > See here:  
> > [http://www.fivesevensix.com/posts/2005/05/20/array-to\_h](http://www.fivesevensix.com/posts/2005/05/20/array-to_h)
> > 
> > ...and here:  
> > [http://drawohara.com/post/70998078/ruby-array-to-hash-rocks](http://drawohara.com/post/70998078/ruby-array-to-hash-rocks)
> > 
> > - Josh

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [1 February 2009 17:44 UTC](https://rubytalk.org/t/array-to-h/51465/9 "2009-02-01T17:44:55Z")

</div>

Or even raise an Exception because Array#to\_h expects a nested structure as is returned from various Hash methods (e.g. #select, #to\_a - note, this is about to change in new versions of Ruby).

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 31.01.2009 21:03, David A. Black wrote:
> 
> > Hi --
> > 
> > On Sun, 1 Feb 2009, Roger Pack wrote:
> > 
> > > Not that I would find it useful at all, but is there is a Hash#to\_a  
> > > should there not be an Array#to\_h?
> > 
> > Not if it's not useful 🙂 It might be, though. It's been talked about  
> > a lot over the years. As I recall, part of the problem is the question  
> > of what it would mean; for example, given this:
> > 
> > &nbsp;&nbsp;&nbsp;["a","b","c","d"].to\_h
> > 
> > is it
> > 
> > &nbsp;&nbsp;&nbsp;["a" =\> "b", "c" =\> "d"]
> > 
> > or
> > 
> > &nbsp;&nbsp;&nbsp;[0 =\> "a", 1 =\> "b", ....]
> > 
> > ?

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [6 February 2009 14:35 UTC](https://rubytalk.org/t/array-to-h/51465/10 "2009-02-06T14:35:23Z")

</div>

> > Not that I would find it useful at all, but is there is a Hash#to\_a  
> > should there not be an Array#to\_h?
> 
> The variety of possible good definitions make this hard to define. So  
> it's understandable that is is not in Ruby core, though being able to  
> convert back and forth between Hash#to\_a and Array#to\_h makes the most  
> sense.
> 
> I was thinking about it some more and I was thinking about how the  
> various options might be addressed. So I came up with this:
> 
> # Converts an associative array into a hash.

\<snip/\>

> By using a +mode+ we can offer a variety of common means of  
> conversion.
> 
> Of course, we could just make them all separate methods, ie. #to\_h,  
> #to\_h\_array, #to\_h\_splat, #to\_h\_flat. Maybe that is better?

This is generally considered better practice over switching behavior  
of a method with an argument. Just think about the length of the  
method which increases in with the number of different algorithms  
(modes). I prefer short methods.

Alternatively make the algorithm detection automatic. Even in that  
case I had to\_h only do the detection and then delegate to any one of  
a number of to\_h\_\<alg\> methods. The rule I follow is to create  
methods and classes to do \_one\_ thing good.

> But then  
> that seems a bit more limiting, less dynamic, less open for new modes  
> or multiple labels for a single mode, and the method names look funny

Not at all: you can simply add another method.

This is how I'd approach it:

module Enumerable  
&nbsp;&nbsp;def to\_h  
&nbsp;&nbsp;&nbsp;&nbsp;pairs = arr = 0

&nbsp;&nbsp;&nbsp;&nbsp;each do |e|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if Array === e  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if e.size \<= 2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pairs += 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr += 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;case  
&nbsp;&nbsp;&nbsp;&nbsp;when pairs == size  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to\_h\_pairs  
&nbsp;&nbsp;&nbsp;&nbsp;when arr \> 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to\_h\_multi  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to\_h\_flat  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

&nbsp;&nbsp;def to\_h\_pairs  
&nbsp;&nbsp;&nbsp;&nbsp;inject({}) {|ha,(k,v)| ha[k]=v; ha}  
&nbsp;&nbsp;end

&nbsp;&nbsp;def to\_h\_multi  
&nbsp;&nbsp;&nbsp;&nbsp;inject({}) {|ha,ar| ha[ar.first] = ar[1..-1]; ha}  
&nbsp;&nbsp;end

&nbsp;&nbsp;def to\_h\_flat  
&nbsp;&nbsp;&nbsp;&nbsp;each\_slice(2).inject({}) {|ha,(k,v)| ha[k]=v; ha}  
&nbsp;&nbsp;end  
end

[  
&nbsp;&nbsp;[1,2,3,4,5],  
&nbsp;&nbsp;[[1,2],[3,4],[5,6]],  
&nbsp;&nbsp;[[1,2],[3,4],[5]],  
&nbsp;&nbsp;[[1,2,3],[4],[5,6]],  
].each do |x|  
&nbsp;&nbsp;p x, x.to\_h, "---"  
end

Kind regards

robert

> **···**
>
> 2009/2/6 Trans \<transfire@gmail.com\>:
> 
> > On Jan 31, 2:49 pm, Roger Pack \<rogerpack2...@gmail.com\> wrote:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![ThoML](https://avatars.discourse-cdn.com/v4/letter/t/d78d45/32.png) [@ThoML](https://rubytalk.org/u/ThoML)\
**Post date:** [6 February 2009 17:52 UTC](https://rubytalk.org/t/array-to-h/51465/11 "2009-02-06T17:52:04Z")

</div>

> As I recall, part of the problem is the question  
> of what it would mean; for example, given this:
> 
> ["a","b","c","d"].to\_h
> 
> is it
> 
> ["a" =\> "b", "c" =\> "d"]

This could be achieved by means of Hash[\*array]:

a = ["a","b","c","d"]  
Hash[\*a]  
=\> {"a"=\>"b", "c"=\>"d"}

---

<div class="post-metadata">

**Author:** ![Julian\_Leviston](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Julian\_Leviston](https://rubytalk.org/u/Julian_Leviston)\
**Post date:** [7 February 2009 00:22 UTC](https://rubytalk.org/t/array-to-h/51465/12 "2009-02-07T00:22:35Z")

</div>

It's fairly trivial, isn't it?

\>\> class Array  
\>\> def to\_h  
\>\> Hash[\*self]  
\>\> end  
=\> nil  
\>\> [1,2,3,4].to\_h  
=\> {1=\>2, 3=\>4}

> **···**
>
> > >
> 
> On 01/02/2009, at 7:03 AM, David A. Black wrote:
> 
> > Hi --
> > 
> > On Sun, 1 Feb 2009, Roger Pack wrote:
> > 
> > > Not that I would find it useful at all, but is there is a Hash#to\_a  
> > > should there not be an Array#to\_h?
> > 
> > Not if it's not useful 🙂 It might be, though. It's been talked about  
> > a lot over the years. As I recall, part of the problem is the question  
> > of what it would mean; for example, given this:
> > 
> > ["a","b","c","d"].to\_h
> > 
> > is it
> > 
> > ["a" =\> "b", "c" =\> "d"]
> > 
> > or
> > 
> > [0 =\> "a", 1 =\> "b", ....]
> > 
> > ?
> > 
> > David
> > 
> > --  
> > David A. Black / Ruby Power and Light, LLC  
> > Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
> > Coming in 2009: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))
> > 
> > [http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [6 February 2009 17:24 UTC](https://rubytalk.org/t/array-to-h/51465/13 "2009-02-06T17:24:31Z")

</div>

> This is generally considered better practice over switching behavior  
> of a method with an argument. Just think about the length of the  
> method which increases in with the number of different algorithms  
> (modes). I prefer short methods.
> 
> Alternatively make the algorithm detection automatic. Even in that  
> case I had to\_h only do the detection and then delegate to any one of  
> a number of to\_h\_\<alg\> methods. The rule I follow is to create  
> methods and classes to do \_one\_ thing good.
> 
> \> But then  
> \> that seems a bit more limiting, less dynamic, less open for new modes  
> \> or multiple labels for a single mode, and the method names look funny
> 
> Not at all: you can simply add another method.

True, but adding a new method is a "bigger deal" than just adding  
another parameter option.

But I like your idea, I could create the different methods and then  
dispatch from to\_h, offering the best of both options.

> This is how I'd approach it:
> 
> module Enumerable  
> def to\_h  
> pairs = arr = 0
> 
> ```
> each do |e|
> if Array === e
> if e\.size &lt;= 2
> pairs \+= 1
> else
> arr \+= 1
> end
> end
> end
> 
> ```

I'm not sure. On one hand I like it, though I am hesitant about it b/c  
it means a whole pass over the array upfront, it won't be very fast.  
What do you think about the performance characteristics? On the other  
hand, it means the one method #to\_h will do quite different things  
depending on the form of the data structure passed to it. Is that a  
good idea?

T.

> **···**
>
> On Feb 6, 9:35 am, Robert Klemme \<shortcut...@googlemail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [7 February 2009 00:26 UTC](https://rubytalk.org/t/array-to-h/51465/14 "2009-02-07T00:26:00Z")

</div>

Hi --

> **···**
>
> On Sat, 7 Feb 2009, Julian Leviston wrote:
> 
> > On 01/02/2009, at 7:03 AM, David A. Black wrote:
> > 
> > > Hi --
> > > 
> > > On Sun, 1 Feb 2009, Roger Pack wrote:
> > > 
> > > > Not that I would find it useful at all, but is there is a Hash#to\_a  
> > > > should there not be an Array#to\_h?
> > > 
> > > Not if it's not useful 🙂 It might be, though. It's been talked about  
> > > a lot over the years. As I recall, part of the problem is the question  
> > > of what it would mean; for example, given this:
> > > 
> > > ["a","b","c","d"].to\_h
> > > 
> > > is it
> > > 
> > > ["a" =\> "b", "c" =\> "d"]
> > > 
> > > or
> > > 
> > > [0 =\> "a", 1 =\> "b", ....]
> > > 
> > > ?
> > 
> > It's fairly trivial, isn't it?
> > 
> > > > class Array  
> > > > def to\_h  
> > > > Hash[\*self]  
> > > > end
> > 
> > =\> nil
> > 
> > > > [1,2,3,4].to\_h
> > 
> > =\> {1=\>2, 3=\>4}
> 
> Yes, if that's the semantics you want. If you're inclined toward  
> thinking it should use the array indices as hash keys and the array  
> values as hash values, then that implementation wouldn't work. My  
> point was that there's been debate about the semantics -- i.e., what  
> Array#to\_h should actually do.
> 
> David
> 
> --  
> David A. Black / Ruby Power and Light, LLC  
> Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
> Coming in 2009: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))
> 
> [http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [6 February 2009 19:04 UTC](https://rubytalk.org/t/array-to-h/51465/15 "2009-02-06T19:04:21Z")

</div>

> > This is generally considered better practice over switching behavior  
> > of a method with an argument. Just think about the length of the  
> > method which increases in with the number of different algorithms  
> > (modes). I prefer short methods.
> > 
> > Alternatively make the algorithm detection automatic. Even in that  
> > case I had to\_h only do the detection and then delegate to any one of  
> > a number of to\_h\_\<alg\> methods. The rule I follow is to create  
> > methods and classes to do \_one\_ thing good.
> > 
> > > But then  
> > > that seems a bit more limiting, less dynamic, less open for new modes  
> > > or multiple labels for a single mode, and the method names look funny
> > 
> > Not at all: you can simply add another method.
> 
> True, but adding a new method is a "bigger deal" than just adding  
> another parameter option.

Not for me.

> But I like your idea, I could create the different methods and then  
> dispatch from to\_h, offering the best of both options.

Exactly.

> > This is how I'd approach it:
> > 
> > module Enumerable  
> > &nbsp;&nbsp;def to\_h  
> > &nbsp;&nbsp;&nbsp;&nbsp;pairs = arr = 0
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;each do |e|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if Array === e  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if e.size \<= 2  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pairs += 1  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr += 1  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> I'm not sure. On one hand I like it, though I am hesitant about it b/c  
> it means a whole pass over the array upfront, it won't be very fast.

But you get automatism. If you know the structure beforehand, you can call one of the other methods. That's the nice thing about this approach: all these methods do one thing good:

> What do you think about the performance characteristics? On the other  
> hand, it means the one method #to\_h will do quite different things  
> depending on the form of the data structure passed to it. Is that a  
> good idea?

Yes. I view it differently: to\_h always makes the same thing which can be described as "analyze contents of the Enum and select a proper algorithm". All other methods do the actual conversions.

If you lump everything into a single method then you cannot use them separately. I would dig up quotes about not using these mode parameters if I had more time right now but I'm in a hurry unfortunately. Trust me, parameters that change a methods behavior are inferior to having separate methods.

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 06.02.2009 18:24, Trans wrote:
> 
> > On Feb 6, 9:35 am, Robert Klemme \<shortcut...@googlemail.com\> wrote:
> 
> --  
> remember.guy do |as, often| as.you\_can - without end

---

<div class="post-metadata">

**Author:** ![Julian\_Leviston](https://avatars.discourse-cdn.com/v4/letter/j/b5a626/32.png) [@Julian\_Leviston](https://rubytalk.org/u/Julian_Leviston)\
**Post date:** [7 February 2009 00:49 UTC](https://rubytalk.org/t/array-to-h/51465/16 "2009-02-07T00:49:23Z")

</div>

Sorry David I've since read the other posts 🙂 there must be SOME good solution to mail catchup. Guess for now I just have to read a whole thread before I reply to any of it 🙂

Blog: [http://random8.zenunit.com/](http://random8.zenunit.com/)  
Learn rails: [http://sensei.zenunit.com/](http://sensei.zenunit.com/)

> **···**
>
> On 07/02/2009, at 11:26 AM, "David A. Black" \<dblack@rubypal.com\> wrote:
> 
> > Hi --
> > 
> > On Sat, 7 Feb 2009, Julian Leviston wrote:
> > 
> > > On 01/02/2009, at 7:03 AM, David A. Black wrote:
> > > 
> > > > Hi --  
> > > > On Sun, 1 Feb 2009, Roger Pack wrote:
> > > > 
> > > > > Not that I would find it useful at all, but is there is a Hash#to\_a  
> > > > > should there not be an Array#to\_h?
> > > > 
> > > > Not if it's not useful 🙂 It might be, though. It's been talked about  
> > > > a lot over the years. As I recall, part of the problem is the question  
> > > > of what it would mean; for example, given this:
> > > > 
> > > > ["a","b","c","d"].to\_h  
> > > > is it
> > > > 
> > > > ["a" =\> "b", "c" =\> "d"]  
> > > > or
> > > > 
> > > > [0 =\> "a", 1 =\> "b", ....]  
> > > > ?
> > > 
> > > It's fairly trivial, isn't it?
> > > 
> > > > > class Array  
> > > > > def to\_h  
> > > > > Hash[\*self]  
> > > > > end
> > > 
> > > =\> nil
> > > 
> > > > > [1,2,3,4].to\_h
> > > 
> > > =\> {1=\>2, 3=\>4}
> > 
> > Yes, if that's the semantics you want. If you're inclined toward  
> > thinking it should use the array indices as hash keys and the array  
> > values as hash values, then that implementation wouldn't work. My  
> > point was that there's been debate about the semantics -- i.e., what  
> > Array#to\_h should actually do.
> > 
> > David
> > 
> > --  
> > David A. Black / Ruby Power and Light, LLC  
> > Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
> > Coming in 2009: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))
> > 
> > [http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!

---

<div class="post-metadata">

**Author:** ![ThoML](https://avatars.discourse-cdn.com/v4/letter/t/d78d45/32.png) [@ThoML](https://rubytalk.org/u/ThoML)\
**Post date:** [7 February 2009 06:22 UTC](https://rubytalk.org/t/array-to-h/51465/17 "2009-02-07T06:22:35Z")

</div>

> Yes, if that's the semantics you want. If you're inclined toward  
> thinking it should use the array indices as hash keys and the array  
> values as hash values, then that implementation wouldn't work.

With that constructor at hand, it wouldn't be too difficult to get  
that behaviour though:

a = [[1,2],[3,4]]

> Hash[(0..(a.size - 1)).zip(a)]

=\> {0=\>[1, 2], 1=\>[3, 4]}

Looks like php.

---

<div class="post-metadata">

**Author:** ![Simon\_Krahnke](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Simon\_Krahnke](https://rubytalk.org/u/Simon_Krahnke)\
**Post date:** [7 February 2009 04:34 UTC](https://rubytalk.org/t/array-to-h/51465/18 "2009-02-07T04:34:20Z")

</div>

\* Robert Klemme \<shortcutter@googlemail.com\> (20:01) schrieb:

> Trust me, parameters that change a methods behavior are inferior to  
> having separate methods.

One can read Robert C. Martin's "Clean Code" for the same advice and  
many more. A devastating read for me.

mfg, simon .... l

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [7 February 2009 12:56 UTC](https://rubytalk.org/t/array-to-h/51465/19 "2009-02-07T12:56:47Z")

</div>

Hi --

> **···**
>
> On Sat, 7 Feb 2009, Tom Link wrote:
> 
> > > Yes, if that's the semantics you want. If you're inclined toward  
> > > thinking it should use the array indices as hash keys and the array  
> > > values as hash values, then that implementation wouldn't work.
> > 
> > With that constructor at hand, it wouldn't be too difficult to get  
> > that behaviour though:
> > 
> > a = [[1,2],[3,4]]
> > 
> > > Hash[(0..(a.size - 1)).zip(a)]
> > 
> > =\> {0=\>[1, 2], 1=\>[3, 4]}
> 
> None of this is hard to do without Array#to\_h, but there's still the  
> question of what Array#to\_h would do if it existed.
> 
> David
> 
> --  
> David A. Black / Ruby Power and Light, LLC  
> Ruby/Rails consulting & training: [http://www.rubypal.com](http://www.rubypal.com)  
> Coming in 2009: The Well-Grounded Rubyist ([http://manning.com/black2\](http://manning.com/black2%5C))
> 
> [http://www.wishsight.com](http://www.wishsight.com) =\> Independent, social wishlist management!
