Partition array to hash in ruby Problem: convert this array of hashes:

cars = [
  { :model=&gt;"Ferrari 458", :speed=&gt;320 },
  { :model=&gt;"Maserati MC12", :speed=&gt;330 },
  { :model=&gt;"Ferrari Enzo", :speed=&gt;350 },
  { :model=&gt;"Lamborghini Huracan", :speed=&gt;325 }
]


to this data structure:

{
  above_320: [
    { :model=&gt;"Maserati MC12", :speed=&gt;330 },
    { :model=&gt;"Lamborghini Huracan", :speed=&gt;325 },
    { :model=&gt;"Ferrari Enzo", :speed=&gt;350 }
  ],
  the_rest: [
    { :model=&gt;"Ferrari 458", :speed=&gt;320 }
  ]
}


My solution:

cars.partition {|car| car[:speed] &gt; 320}
.map.with_index {|cars,i| [ i == 0 ? :above_320 : :the_rest, cars ]}
.to_h


Any feedback would be greatly appreciated!
I would go with the chunk method:

cars.chunk { |c| c[:speed]&gt;320 ? :above_320 : :the_rest }.to_h


Though it's important to underline that chunk doesn't work if the array is not ordered (by speed in this case). In that case we need to prepend it with sort_by:

cars.sort_by {|car| car[:speed]}.chunk { |c| c[:speed]&gt;320 ? :above_320 : :the_rest }.to_h


(Moreover, if we want to order the hash keys, like in the example, we need to call sort after chunk):

cars.sort_by {|car| car[:speed]}.chunk { |c| c[:speed]&gt;320 ? :above_320 : :the_rest }.sort.to_h