title: How to make Ruby hash of arrays from array
body: <p>I'm a Perl developer trying to learn Ruby...
  So, I'll demonstrate in Perl what I'm trying to accomplish in Ruby and then summarize at the end...</p>
  
  <pre><code>#!/usr/bin/perl -w
  
  use strict;
  use Data::Dumper;
  
  # Given the following data structure (an array of hashes)
  my $j  = [ 
              {   
                  id   =&gt; 1,
                  key1 =&gt; 'name1',
                  key2 =&gt; 'data',
                  key3 =&gt; 'data',
                  key4 =&gt; 'foo',
              },  
              {   
                  id   =&gt; 2,
                  key1 =&gt; 'name1',
                  key2 =&gt; 'data',
                  key3 =&gt; 'data',
                  key4 =&gt; 'bar',
              },  
              {   
                  id   =&gt; 3,
                  key1 =&gt; 'name2',
                  key2 =&gt; 'data',
                  key3 =&gt; 'data',
                  key4 =&gt; 'baz',
              },  
          ];  
  
  print ~~@$j,"\n";
  print Dumper($j)."\n";
  
  my $myHash; # make it a reference to a hoa.
  
  for my $array ( @{$j} )
  {
     # the key to my new key-name is always known
      push(@{$myHash-&gt;{$array-&gt;{key1}}},$array-&gt;{key4});
  }
  
  print Dumper($myHash)."\n";
  </code></pre>
  
  <p>And the output:</p>
  
  <p>Initial array:</p>
  
  <pre><code>$VAR1 = [
            {
              'key2' =&gt; 'data',
              'key4' =&gt; 'foo',
              'key1' =&gt; 'name1',
              'id' =&gt; 1,
              'key3' =&gt; 'data'
            },
            {
              'key2' =&gt; 'data',
              'key4' =&gt; 'bar',
              'key1' =&gt; 'name1',
              'id' =&gt; 2,
              'key3' =&gt; 'data'
            },
            {
              'key2' =&gt; 'data',
              'key4' =&gt; 'baz',
              'key1' =&gt; 'name2',
              'id' =&gt; 3,
              'key3' =&gt; 'data'
            }
          ];
  </code></pre>
  
  <p>What I'm trying to get:</p>
  
  <pre><code>$VAR1 = {
            'name2' =&gt; [
                         'baz'
                       ],
            'name1' =&gt; [
                         'foo',
                         'bar'
                       ]
          };
  </code></pre>
  
  <p>...and I'm trying to do it in as succinct code as possible, which has proven to be a pain given my lack of Ruby goodness. I've tried multiple attempts at this including several tries at map similar to the same code structure I used in Perl to no avail.</p>
  
  <p>That said, I did find the following snippet, just now, which almost works but I'm sure I'm doing something wrong...</p>
  
  <pre><code>h = Hash[j.collect {|array| [array.key1,array.key4] }]
  </code></pre>
  
  <p>This gets me the right hash key but it doesn't push the key4 value into an array of the hash. Still looking but some nudge in the right direction would be appreciated. If I find the answer before getting an answer here, I'll answer the question for the edification of future readers.</p>
  
  <p>EDIT!
  I need to clarify something that I just figured out and is probably the wrench in my clock. The data that I'm getting is not a pure array. It's actually an object from DataMapper.</p>