Question: How can I transform `array1` into `hash1` in Ruby?

`array1` looks like this: 

```
    [[80, "X", 12],
     [80, "X", 13],
     [80, "X", 14],
     [80, "X", 15],
     [80, "X", 16],
     [81, "Y", 20],
     [81, "Y", 21],
     [81, "Y", 22],
     [81, "Y", 23],
     [81, "Y", 24]]

```

`hash1` looks like this:

```
[['id' => 80, 'type' = >'X', numbers => {12,13,14,15,16}],
       ['id' => 81, 'type' = >'Y',numbers => {20,21,22,23,24}]]

```

Answer: 
Whenever [Enumerable#group\_by](http://ruby-doc.org/core-2.2.0/Enumerable.html#method-i-group_by) is used (as by @sawa), one can alternatively use the form of [Hash#update](http://ruby-doc.org/core-2.2.0/Hash.html#method-i-update) (aka `merge!`) that employs a block to determine the values of keys that are present in both hashes being merged:

```
arr = [[80, "X", 12],
       [80, "X", 13],
       [80, "X", 14],
       [80, "X", 15],
       [80, "X", 16],
       [81, "Y", 20],
       [81, "Y", 21],
       [81, "Y", 22],
       [81, "Y", 23],
       [81, "Y", 24]]

arr.each_with_object({}) { |(x,y,z),h|
  h.update(x=>{ "id"=>x, "type"=>y, "numbers"=>[z] }) { |_,o,n|
    { "id"=>o["id"], "type"=>o["type"], "numbers"=>o["numbers"]+n["numbers"] } } }.values
    #=> [{"id"=>80, "type"=>"X", "numbers"=>[12, 13, 14, 15, 16]},
    #    {"id"=>81, "type"=>"Y", "numbers"=>[20, 21, 22, 23, 24]}] 

```
You would have to iterate through your array1 and start assigning values into the hash that you want using basic comparisons (if - else, group\_by and mapping) and keeping history of the previous ids.

There's no such a ruby method that does this "magically".