Problem: convert this array of hashes:
cars = [
{ :model=>"Ferrari 458", :speed=>320 },
{ :model=>"Maserati MC12", :speed=>330 },
{ :model=>"Ferrari Enzo", :speed=>350 },
{ :model=>"Lamborghini Huracan", :speed=>325 }
]
to this data structure:
{
above_320: [
{ :model=>"Maserati MC12", :speed=>330 },
{ :model=>"Lamborghini Huracan", :speed=>325 },
{ :model=>"Ferrari Enzo", :speed=>350 }
],
the_rest: [
{ :model=>"Ferrari 458", :speed=>320 }
]
}
My solution:
cars.partition {|car| car[:speed] > 320}
.map.with_index {|cars,i| [ i == 0 ? :above_320 : :the_rest, cars ]}
.to_h
Any feedback would be greatly appreciated!