<p>In Ruby, given an array in one of the following forms...</p>

<pre><code>[apple, 1, banana, 2]
[[apple, 1], [banana, 2]]
</code></pre>

<p>...what is the best way to convert this into a hash in the form of...</p>

<pre><code>{apple =&gt; 1, banana =&gt; 2}
</code></pre>

[{'answer_id': 39621, 'author': 'John Topley', 'author_id': 1450, 'author_profile': 'https://Stackoverflow.com/users/1450', 'pm_score': 7, 'selected': False, 'text': '<p>Simply use <code>Hash[*array_variable.flatten]</code></p>\n\n<p>For example:</p>\n\n<pre><code>a1 = [\'apple\', 1, \'banana\', 2]\nh1 = Hash[*a1.flatten(1)]\nputs "h1: #{h1.inspect}"\n\na2 = [[\'apple\', 1], [\'banana\', 2]]\nh2 = Hash[*a2.flatten(1)]\nputs "h2: #{h2.inspect}"\n</code></pre>\n\n<p>Using <code>Array#flatten(1)</code> limits the recursion so <code>Array</code> keys and values work as expected.</p>\n'}, {'answer_id': 39625, 'author': 'Dominik Grabiec', 'author_id': 3719, 'author_profile': 'https://Stackoverflow.com/users/3719', 'pm_score': 3, 'selected': False, 'text': "<blockquote>\n  <p>Edit: Saw the responses posted while I was writing, Hash[a.flatten] seems the way to go.\n  Must have missed that bit in the documentation when I was thinking through the response. Thought the solutions that I've written can be used as alternatives if required.</p>\n</blockquote>\n\n<p>The second form is simpler:</p>\n\n<pre><code>a = [[:apple, 1], [:banana, 2]]\nh = a.inject({}) { |r, i| r[i.first] = i.last; r }\n</code></pre>\n\n<p>a = array, h = hash, r = return-value hash (the one we accumulate in), i = item in the array</p>\n\n<p>The neatest way that I can think of doing the first form is something like this:</p>\n\n<pre><code>a = [:apple, 1, :banana, 2]\nh = {}\na.each_slice(2) { |i| h[i.first] = i.last }\n</code></pre>\n"}, {'answer_id': 39629, 'author': 'Anders Sandvig', 'author_id': 1709, 'author_profile': 'https://Stackoverflow.com/users/1709', 'pm_score': 0, 'selected': False, 'text': '<p>Not sure if it\'s the best way, but this works:</p>\n\n<pre><code>a = ["apple", 1, "banana", 2]\nm1 = {}\nfor x in (a.length / 2).times\n  m1[a[x*2]] = a[x*2 + 1]\nend\n\nb = [["apple", 1], ["banana", 2]]\nm2 = {}\nfor x,y in b\n  m2[x] = y\nend\n</code></pre>\n'}, {'answer_id': 39641, 'author': 'Gishu', 'author_id': 1695, 'author_profile': 'https://Stackoverflow.com/users/1695', 'pm_score': -1, 'selected': False, 'text': '<p>If the numeric values are seq indexes, then we could have simpler ways... \nHere\'s my code submission, My Ruby is a bit rusty</p>\n\n<pre><code>   input = ["cat", 1, "dog", 2, "wombat", 3]\n   hash = Hash.new\n   input.each_with_index {|item, index|\n     if (index%2 == 0) hash[item] = input[index+1]\n   }\n   hash   #=&gt; {"cat"=&gt;1, "wombat"=&gt;3, "dog"=&gt;2}\n</code></pre>\n'}, {'answer_id': 3056424, 'author': 'StevenJenkins', 'author_id': 210672, 'author_profile': 'https://Stackoverflow.com/users/210672', 'pm_score': 2, 'selected': False, 'text': '<p>Appending to the answer but using anonymous arrays and annotating:</p>\n\n<pre><code>Hash[*("a,b,c,d".split(\',\').zip([1,2,3,4]).flatten)]\n</code></pre>\n\n<p>Taking that answer apart, starting from the inside:</p>\n\n<ul>\n<li><code>"a,b,c,d"</code> is actually a string.</li>\n<li><code>split</code> on commas into an array.</li>\n<li><code>zip</code> that together with the following array.</li>\n<li><code>[1,2,3,4]</code> is an actual array.</li>\n</ul>\n\n<p>The intermediate result is:</p>\n\n<pre><code>[[a,1],[b,2],[c,3],[d,4]]\n</code></pre>\n\n<p>flatten then transforms that to:</p>\n\n<pre><code>["a",1,"b",2,"c",3,"d",4]\n</code></pre>\n\n<p>and then:</p>\n\n<p><code>*["a",1,"b",2,"c",3,"d",4]</code> unrolls that into\n<code>"a",1,"b",2,"c",3,"d",4</code></p>\n\n<p>which we can use as the arguments to the <code>Hash[]</code> method:</p>\n\n<pre><code>Hash[*("a,b,c,d".split(\',\').zip([1,2,3,4]).flatten)]\n</code></pre>\n\n<p>which yields:</p>\n\n<pre><code>{"a"=&gt;1, "b"=&gt;2, "c"=&gt;3, "d"=&gt;4}\n</code></pre>\n'}, {'answer_id': 9571767, 'author': 'Stew', 'author_id': 332936, 'author_profile': 'https://Stackoverflow.com/users/332936', 'pm_score': 8, 'selected': True, 'text': '<p><strong>NOTE</strong>: For a concise and efficient solution, please see <a href="https://stackoverflow.com/a/20831486/332936">Marc-André Lafortune\'s answer</a> below.</p>\n\n<p>This answer was originally offered as an alternative to approaches using flatten, which were the most highly upvoted at the time of writing. I should have clarified that I didn\'t intend to present this example as a best practice or an efficient approach. Original answer follows.</p>\n\n<hr>\n\n<p><strong>Warning!</strong> Solutions using <strong>flatten</strong> will not preserve Array keys or values!</p>\n\n<p>Building on @John Topley\'s popular answer, let\'s try:</p>\n\n<pre><code>a3 = [ [\'apple\', 1], [\'banana\', 2], [[\'orange\',\'seedless\'], 3] ]\nh3 = Hash[*a3.flatten]\n</code></pre>\n\n<p>This throws an error:</p>\n\n<pre><code>ArgumentError: odd number of arguments for Hash\n        from (irb):10:in `[]\'\n        from (irb):10\n</code></pre>\n\n<p>The constructor was expecting an Array of even length (e.g. [\'k1\',\'v1,\'k2\',\'v2\']).  What\'s worse is that a different Array which flattened to an even length would just silently give us a Hash with incorrect values.</p>\n\n<p>If you want to use Array keys or values, you can use <strong>map</strong>:</p>\n\n<pre><code>h3 = Hash[a3.map {|key, value| [key, value]}]\nputs "h3: #{h3.inspect}"\n</code></pre>\n\n<p>This preserves the Array key:</p>\n\n<pre><code>h3: {["orange", "seedless"]=&gt;3, "apple"=&gt;1, "banana"=&gt;2}\n</code></pre>\n'}, {'answer_id': 14279597, 'author': 'Priyanka', 'author_id': 1970032, 'author_profile': 'https://Stackoverflow.com/users/1970032', 'pm_score': 3, 'selected': False, 'text': '<p>You can also simply convert a 2D array into hash using:</p>\n\n<pre><code>1.9.3p362 :005 &gt; a= [[1,2],[3,4]]\n\n =&gt; [[1, 2], [3, 4]]\n\n1.9.3p362 :006 &gt; h = Hash[a]\n\n =&gt; {1=&gt;2, 3=&gt;4} \n</code></pre>\n'}, {'answer_id': 20777227, 'author': 'JanDintel', 'author_id': 2111438, 'author_profile': 'https://Stackoverflow.com/users/2111438', 'pm_score': 5, 'selected': False, 'text': '<p><strong>Update</strong></p>\n\n<p><a href="http://www.ruby-lang.org/en/news/2013/12/25/ruby-2-1-0-is-released/">Ruby 2.1.0 is released today</a>. And I comes with <code>Array#to_h</code> (<a href="https://github.com/ruby/ruby/blob/v2_1_0/NEWS#L34">release notes</a> and <a href="http://ruby-doc.org/core-2.1.0/Array.html#method-i-to_h">ruby-doc</a>), which solves the issue of converting an <code>Array</code> to a <code>Hash</code>. </p>\n\n<p>Ruby docs example:</p>\n\n<pre><code>[[:foo, :bar], [1, 2]].to_h    # =&gt; {:foo =&gt; :bar, 1 =&gt; 2}\n</code></pre>\n'}, {'answer_id': 20831486, 'author': 'Marc-André Lafortune', 'author_id': 8279, 'author_profile': 'https://Stackoverflow.com/users/8279', 'pm_score': 7, 'selected': False, 'text': '<p>The best way is to use <a href="http://ruby-doc.org/core-2.6.1/Array.html#method-i-to_h" rel="noreferrer"><code>Array#to_h</code></a>:</p>\n\n<pre><code>[ [:apple,1],[:banana,2] ].to_h  #=&gt; {apple: 1, banana: 2}\n</code></pre>\n\n<p>Note that <code>to_h</code> also accepts a block:</p>\n\n<pre><code>[:apple, :banana].to_h { |fruit| [fruit, "I like #{fruit}s"] } \n  # =&gt; {apple: "I like apples", banana: "I like bananas"}\n</code></pre>\n\n<p><strong>Note</strong>: <code>to_h</code> accepts a block in Ruby 2.6.0+; for early rubies you can use my <code>backports</code> gem and <a href="http://github.com/marcandre/backports" rel="noreferrer"><code>require \'backports/2.6.0/enumerable/to_h\'</code></a></p>\n\n<p><code>to_h</code> without a block was introduced in Ruby 2.1.0.</p>\n\n<p>Before Ruby 2.1, one could use the less legible <code>Hash[]</code>:</p>\n\n<pre><code>array = [ [:apple,1],[:banana,2] ]\nHash[ array ]  #= &gt; {:apple =&gt; 1, :banana =&gt; 2}\n</code></pre>\n\n<p>Finally, be wary of any solutions using <code>flatten</code>, this could create problems with values that are arrays themselves.</p>\n'}, {'answer_id': 29008534, 'author': 'user3588841', 'author_id': 3588841, 'author_profile': 'https://Stackoverflow.com/users/3588841', 'pm_score': 0, 'selected': False, 'text': '<p>if you have array that looks like this -</p>\n\n<pre><code>data = [["foo",1,2,3,4],["bar",1,2],["foobar",1,"*",3,5,:foo]]\n</code></pre>\n\n<p>and you want the first elements of each array to become the keys for the hash and the rest of the elements becoming value arrays, then you can do something like this -</p>\n\n<pre><code>data_hash = Hash[data.map { |key| [key.shift, key] }]\n\n#=&gt;{"foo"=&gt;[1, 2, 3, 4], "bar"=&gt;[1, 2], "foobar"=&gt;[1, "*", 3, 5, :foo]}\n</code></pre>\n'}, {'answer_id': 47614488, 'author': 'lindes', 'author_id': 313756, 'author_profile': 'https://Stackoverflow.com/users/313756', 'pm_score': 3, 'selected': False, 'text': '<h1>Summary &amp; TL;DR:</h1>\n\n<p>This answer hopes to be a comprehensive wrap-up of information from other answers.</p>\n\n<p>The very short version, given the data from the question plus a couple extras:</p>\n\n<pre><code>flat_array   = [  apple, 1,   banana, 2  ] # count=4\nnested_array = [ [apple, 1], [banana, 2] ] # count=2 of count=2 k,v arrays\nincomplete_f = [  apple, 1,   banana     ] # count=3 - missing last value\nincomplete_n = [ [apple, 1], [banana   ] ] # count=2 of either k or k,v arrays\n\n\n# there\'s one option for flat_array:\nh1  = Hash[*flat_array]                     # =&gt; {apple=&gt;1, banana=&gt;2}\n\n# two options for nested_array:\nh2a = nested_array.to_h # since ruby 2.1.0    =&gt; {apple=&gt;1, banana=&gt;2}\nh2b = Hash[nested_array]                    # =&gt; {apple=&gt;1, banana=&gt;2}\n\n# ok if *only* the last value is missing:\nh3  = Hash[incomplete_f.each_slice(2).to_a] # =&gt; {apple=&gt;1, banana=&gt;nil}\n# always ok for k without v in nested array:\nh4  = Hash[incomplete_n] # or .to_h           =&gt; {apple=&gt;1, banana=&gt;nil}\n\n# as one might expect:\nh1 == h2a # =&gt; true\nh1 == h2b # =&gt; true\nh1 == h3  # =&gt; false\nh3 == h4  # =&gt; true\n</code></pre>\n\n<p>Discussion and details follow.</p>\n\n<hr>\n\n<h1>Setup: variables</h1>\n\n<p>In order to show the data we\'ll be using up front, I\'ll create some variables to represent various possibilities for the data. They fit into the following categories:</p>\n\n<h3>Based on what was directly in the question, as <code>a1</code> and <code>a2</code>:</h3>\n\n<p><i>(Note: I presume that <code>apple</code> and <code>banana</code> were meant to represent variables. As others have done, I\'ll be using strings from here on so that input and results can match.)</i></p>\n\n\n\n<pre class="lang-ruby prettyprint-override"><code>a1 = [  \'apple\', 1 ,  \'banana\', 2  ] # flat input\na2 = [ [\'apple\', 1], [\'banana\', 2] ] # key/value paired input\n</code></pre>\n\n<h3>Multi-value keys and/or values, as <code>a3</code>:</h3>\n\n<p>In some other answers, another possibility was presented (which I expand on here) – keys and/or values may be arrays on their own:</p>\n\n<pre class="lang-ruby prettyprint-override"><code>a3 = [ [ \'apple\',                   1   ],\n       [ \'banana\',                  2   ],\n       [ [\'orange\',\'seedless\'],     3   ],\n       [ \'pear\',                 [4, 5] ],\n     ]\n</code></pre>\n\n<h3>Unbalanced array, as <code>a4</code>:</h3>\n\n<p>For good measure, I thought I\'d add one for a case where we might have an incomplete input:</p>\n\n<pre class="lang-ruby prettyprint-override"><code>a4 = [ [ \'apple\',                   1],\n       [ \'banana\',                  2],\n       [ [\'orange\',\'seedless\'],     3],\n       [ \'durian\'                    ], # a spiky fruit pricks us: no value!\n     ]\n</code></pre>\n\n<hr>\n\n<h1>Now, to work:</h1>\n\n<h2>Starting with an initially-flat array, <code>a1</code>:</h2>\n\n<p>Some have suggested using <a href="https://ruby-doc.org/core/Array.html#method-i-to_h" rel="noreferrer"><code>#to_h</code></a> (which showed up in Ruby 2.1.0, and can be <a href="https://github.com/marcandre/backports" rel="noreferrer">backported</a> to earlier versions). For an initially-flat array, this doesn\'t work:</p>\n\n<pre class="lang-ruby prettyprint-override"><code>a1.to_h   # =&gt; TypeError: wrong element type String at 0 (expected array)\n</code></pre>\n\n<p>Using <a href="https://ruby-doc.org/core/Hash.html#method-c-5B-5D" rel="noreferrer"><code>Hash::[]</code></a> combined with the <a href="https://ruby-doc.org/core/doc/syntax/calling_methods_rdoc.html#label-Array+to+Arguments+Conversion" rel="noreferrer">splat operator</a> does:</p>\n\n<pre class="lang-ruby prettyprint-override"><code>Hash[*a1] # =&gt; {"apple"=&gt;1, "banana"=&gt;2}\n</code></pre>\n\n<p>So that\'s the solution for the simple case represented by <code>a1</code>.</p>\n\n<h2>With an array of key/value pair arrays, <code>a2</code>:</h2>\n\n<p>With an array of <code>[key,value]</code> type arrays, there are two ways to go.</p>\n\n<p>First, <code>Hash::[]</code> still works (as it did with <code>*a1</code>):</p>\n\n<pre class="lang-ruby prettyprint-override"><code>Hash[a2] # =&gt; {"apple"=&gt;1, "banana"=&gt;2}\n</code></pre>\n\n<p>And then also <code>#to_h</code> works now:</p>\n\n<pre class="lang-ruby prettyprint-override"><code>a2.to_h  # =&gt; {"apple"=&gt;1, "banana"=&gt;2}\n</code></pre>\n\n<p>So, two easy answers for the simple nested array case.</p>\n\n<h3>This remains true even with sub-arrays as keys or values, as with <code>a3</code>:</h3>\n\n<pre class="lang-ruby prettyprint-override"><code>Hash[a3] # =&gt; {"apple"=&gt;1, "banana"=&gt;2, ["orange", "seedless"]=&gt;3, "pear"=&gt;[4, 5]} \na3.to_h  # =&gt; {"apple"=&gt;1, "banana"=&gt;2, ["orange", "seedless"]=&gt;3, "pear"=&gt;[4, 5]}\n</code></pre>\n\n<h2>But durians have spikes (anomalous structures give problems):</h2>\n\n<p>If we\'ve gotten input data that\'s not balanced, we\'ll run into problems with <code>#to_h</code>:</p>\n\n<pre class="lang-ruby prettyprint-override"><code>a4.to_h  # =&gt; ArgumentError: wrong array length at 3 (expected 2, was 1)\n</code></pre>\n\n<p>But <code>Hash::[]</code> still works, just setting <code>nil</code> as the value for <code>durian</code> (and any other array element in a4 that\'s just a 1-value array):</p>\n\n<pre class="lang-ruby prettyprint-override"><code>Hash[a4] # =&gt; {"apple"=&gt;1, "banana"=&gt;2, ["orange", "seedless"]=&gt;3, "durian"=&gt;nil}\n</code></pre>\n\n<h3>Flattening - using new variables <code>a5</code> and <code>a6</code></h3>\n\n<p>A few other answers mentioned <a href="https://ruby-doc.org/core/Array.html#method-i-flatten" rel="noreferrer"><code>flatten</code></a>, with or without a <code>1</code> argument, so let\'s create some new variables:</p>\n\n<pre class="lang-ruby prettyprint-override"><code>a5 = a4.flatten\n# =&gt; ["apple", 1, "banana", 2,  "orange", "seedless" , 3, "durian"] \na6 = a4.flatten(1)\n# =&gt; ["apple", 1, "banana", 2, ["orange", "seedless"], 3, "durian"] \n</code></pre>\n\n<p>I chose to use <code>a4</code> as the base data because of the balance problem we had, which showed up with <code>a4.to_h</code>.  I figure calling <code>flatten</code> might be one approach someone might use to try to solve that, which might look like the following.</p>\n\n<h3><code>flatten</code> without arguments (<code>a5</code>):</h3>\n\n<pre class="lang-ruby prettyprint-override"><code>Hash[*a5]       # =&gt; {"apple"=&gt;1, "banana"=&gt;2, "orange"=&gt;"seedless", 3=&gt;"durian"}\n# (This is the same as calling `Hash[*a4.flatten]`.)\n</code></pre>\n\n<p>At a naïve glance, this appears to work – but it got us off on the wrong foot with the seedless oranges, thus also making <code>3</code> a <em>key</em> and <code>durian</code> a <em>value</em>.</p>\n\n<p>And this, as with <code>a1</code>, just doesn\'t work:</p>\n\n<pre class="lang-ruby prettyprint-override"><code>a5.to_h # =&gt; TypeError: wrong element type String at 0 (expected array)\n</code></pre>\n\n<p>So <code>a4.flatten</code> isn\'t useful to us, we\'d just want to use <code>Hash[a4]</code></p>\n\n<h3>The <code>flatten(1)</code> case (<code>a6</code>):</h3>\n\n<p>But what about only partially flattening?  It\'s worth noting that calling <code>Hash::[]</code> using <code>splat</code> on the partially-flattened array (<code>a6</code>) is <em>not</em> the same as calling <code>Hash[a4]</code>:</p>\n\n<pre class="lang-ruby prettyprint-override"><code>Hash[*a6] # =&gt; ArgumentError: odd number of arguments for Hash\n</code></pre>\n\n<h3>Pre-flattened array, still nested (alternate way of getting <code>a6</code>):</h3>\n\n<p>But what if this was how we\'d gotten the array in the first place?\n (That is, comparably to <code>a1</code>, it was our input data - just this time some of the data can be arrays or other objects.)  We\'ve seen that <code>Hash[*a6]</code> doesn\'t work, but what if we still wanted to get the behavior where the <em>last element</em> (important! see below) acted as a key for a <code>nil</code> value?</p>\n\n<p>In such a situation, there\'s still a way to do this, using <a href="https://ruby-doc.org/core/Enumerable.html#method-i-each_slice" rel="noreferrer"><code>Enumerable#each_slice</code></a> to get ourselves back to key/value <em>pairs</em> as elements in the outer array:</p>\n\n<pre class="lang-ruby prettyprint-override"><code>a7 = a6.each_slice(2).to_a\n# =&gt; [["apple", 1], ["banana", 2], [["orange", "seedless"], 3], ["durian"]] \n</code></pre>\n\n<p>Note that this ends up getting us a new array that isn\'t "<a href="https://ruby-doc.org/core/BasicObject.html#method-i-equal-3F" rel="noreferrer">identical</a>" to <code>a4</code>, but does have the <a href="https://ruby-doc.org/core/Array.html#method-i-3D-3D" rel="noreferrer">same values</a>:</p>\n\n<pre class="lang-ruby prettyprint-override"><code>a4.equal?(a7) # =&gt; false\na4 == a7      # =&gt; true\n</code></pre>\n\n<p>And thus we can again use <code>Hash::[]</code>:</p>\n\n<pre class="lang-ruby prettyprint-override"><code>Hash[a7] # =&gt; {"apple"=&gt;1, "banana"=&gt;2, ["orange", "seedless"]=&gt;3, "durian"=&gt;nil}\n# or Hash[a6.each_slice(2).to_a]\n</code></pre>\n\n<h3>But there\'s a problem!</h3>\n\n<p>It\'s important to note that the <code>each_slice(2)</code> solution only gets things back to sanity if the <strong><em>last</em></strong> key was the one missing a value.  If we later added an extra key/value pair:</p>\n\n<pre class="lang-ruby prettyprint-override"><code>a4_plus = a4.dup # just to have a new-but-related variable name\na4_plus.push([\'lychee\', 4])\n# =&gt; [["apple",                1],\n#     ["banana",               2],\n#     [["orange", "seedless"], 3], # multi-value key\n#     ["durian"],              # missing value\n#     ["lychee", 4]]           # new well-formed item\n\na6_plus = a4_plus.flatten(1)\n# =&gt; ["apple", 1, "banana", 2, ["orange", "seedless"], 3, "durian", "lychee", 4]\n\na7_plus = a6_plus.each_slice(2).to_a\n# =&gt; [["apple",                1],\n#     ["banana",               2],\n#     [["orange", "seedless"], 3], # so far so good\n#     ["durian",               "lychee"], # oops! key became value!\n#     [4]]                     # and we still have a key without a value\n\na4_plus == a7_plus # =&gt; false, unlike a4 == a7\n</code></pre>\n\n<p>And the two hashes we\'d get from this are different in important ways:</p>\n\n<pre class="lang-ruby prettyprint-override"><code>ap Hash[a4_plus] # prints:\n{\n                     "apple" =&gt; 1,\n                    "banana" =&gt; 2,\n    [ "orange", "seedless" ] =&gt; 3,\n                    "durian" =&gt; nil, # correct\n                    "lychee" =&gt; 4    # correct\n}\n\nap Hash[a7_plus] # prints:\n{\n                     "apple" =&gt; 1,\n                    "banana" =&gt; 2,\n    [ "orange", "seedless" ] =&gt; 3,\n                    "durian" =&gt; "lychee", # incorrect\n                           4 =&gt; nil       # incorrect\n}\n</code></pre>\n\n<p><i>(Note: I\'m using <a href="https://github.com/awesome-print/awesome_print" rel="noreferrer"><code>awesome_print</code></a>\'s <code>ap</code> just to make it easier to show the structure here; there\'s no conceptual requirement for this.)</i></p>\n\n<p>So the <code>each_slice</code> solution to an unbalanced flat input only works if the unbalanced bit is at the very end.</p>\n\n<hr>\n\n<h1>Take-aways:</h1>\n\n<ol>\n<li>Whenever possible, set up input to these things as <code>[key, value]</code> pairs (a sub-array for each item in the outer array).</li>\n<li>When you can indeed do that, either <code>#to_h</code> or <code>Hash::[]</code> will both work.</li>\n<li>If you\'re unable to, <code>Hash::[]</code> combined with the splat (<code>*</code>) will work, <em>so long as inputs are balanced</em>.</li>\n<li>With an <em>unbalanced</em> and <em>flat</em> array as input, the only way this will work at all reasonably is if the <strong><em>last</em></strong> <code>value</code> item is the only one that\'s missing.</li>\n</ol>\n\n<hr>\n\n<p><i>Side-note: I\'m posting this answer because I feel there\'s value to be added – some of the existing answers have incorrect information, and none (that I read) gave as complete an answer as I\'m endeavoring to do here.  I hope that it\'s helpful.  I nevertheless give thanks to those who came before me, several of whom provided inspiration for portions of this answer.</i></p>\n'}, {'answer_id': 68745099, 'author': 'noraj', 'author_id': 5511315, 'author_profile': 'https://Stackoverflow.com/users/5511315', 'pm_score': 0, 'selected': False, 'text': '<p>For performance and memory allocation concerns please check <a href="https://stackoverflow.com/questions/11856407/rails-mapping-array-of-hashes-onto-single-hash/68745019#answer-68745019">my answer</a> to <a href="https://stackoverflow.com/questions/11856407/rails-mapping-array-of-hashes-onto-single-hash/68745019">Rails mapping array of hashes onto single hash</a> where I bench-marked several solutions.</p>\n<p><code>reduce</code> / <code>inject</code> can be the fastest or the slowest solution depending on which method you use it which.</p>\n'}]
['https://Stackoverflow.com/questions/39567', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4142/']