Partition array to hash in ruby
up vote
1
down vote
favorite
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!
ruby
New contributor
add a comment |
up vote
1
down vote
favorite
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!
ruby
New contributor
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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!
ruby
New contributor
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!
ruby
ruby
New contributor
New contributor
New contributor
asked yesterday
sekmo
1135
1135
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
I would go with the chunk
method:
cars.chunk { |c| c[:speed]>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]>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]>320 ? :above_320 : :the_rest }.sort.to_h
Thanks! It's a thing that popped up from a pet project actually :-)
– sekmo
yesterday
add a comment |
up vote
1
down vote
I think that .map.with_index {|cars,i| [ i == 0 ? :above_320 : :the_rest, cars ]}
is a bit more verbose and awkward than it needs to be.
Hash[
[:above_320, :the_rest].zip(cars.partition { |car| car[:speed] > 320 })
]
Alternatively,
[:above_320, :the_rest]
.zip(cars.partition { |car| car[:speed] > 320 })
.to_h
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
I would go with the chunk
method:
cars.chunk { |c| c[:speed]>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]>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]>320 ? :above_320 : :the_rest }.sort.to_h
Thanks! It's a thing that popped up from a pet project actually :-)
– sekmo
yesterday
add a comment |
up vote
3
down vote
accepted
I would go with the chunk
method:
cars.chunk { |c| c[:speed]>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]>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]>320 ? :above_320 : :the_rest }.sort.to_h
Thanks! It's a thing that popped up from a pet project actually :-)
– sekmo
yesterday
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
I would go with the chunk
method:
cars.chunk { |c| c[:speed]>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]>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]>320 ? :above_320 : :the_rest }.sort.to_h
I would go with the chunk
method:
cars.chunk { |c| c[:speed]>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]>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]>320 ? :above_320 : :the_rest }.sort.to_h
edited 22 hours ago
sekmo
1135
1135
answered yesterday
Marc Rohloff
2,93236
2,93236
Thanks! It's a thing that popped up from a pet project actually :-)
– sekmo
yesterday
add a comment |
Thanks! It's a thing that popped up from a pet project actually :-)
– sekmo
yesterday
Thanks! It's a thing that popped up from a pet project actually :-)
– sekmo
yesterday
Thanks! It's a thing that popped up from a pet project actually :-)
– sekmo
yesterday
add a comment |
up vote
1
down vote
I think that .map.with_index {|cars,i| [ i == 0 ? :above_320 : :the_rest, cars ]}
is a bit more verbose and awkward than it needs to be.
Hash[
[:above_320, :the_rest].zip(cars.partition { |car| car[:speed] > 320 })
]
Alternatively,
[:above_320, :the_rest]
.zip(cars.partition { |car| car[:speed] > 320 })
.to_h
add a comment |
up vote
1
down vote
I think that .map.with_index {|cars,i| [ i == 0 ? :above_320 : :the_rest, cars ]}
is a bit more verbose and awkward than it needs to be.
Hash[
[:above_320, :the_rest].zip(cars.partition { |car| car[:speed] > 320 })
]
Alternatively,
[:above_320, :the_rest]
.zip(cars.partition { |car| car[:speed] > 320 })
.to_h
add a comment |
up vote
1
down vote
up vote
1
down vote
I think that .map.with_index {|cars,i| [ i == 0 ? :above_320 : :the_rest, cars ]}
is a bit more verbose and awkward than it needs to be.
Hash[
[:above_320, :the_rest].zip(cars.partition { |car| car[:speed] > 320 })
]
Alternatively,
[:above_320, :the_rest]
.zip(cars.partition { |car| car[:speed] > 320 })
.to_h
I think that .map.with_index {|cars,i| [ i == 0 ? :above_320 : :the_rest, cars ]}
is a bit more verbose and awkward than it needs to be.
Hash[
[:above_320, :the_rest].zip(cars.partition { |car| car[:speed] > 320 })
]
Alternatively,
[:above_320, :the_rest]
.zip(cars.partition { |car| car[:speed] > 320 })
.to_h
edited yesterday
answered yesterday
200_success
127k15148410
127k15148410
add a comment |
add a comment |
sekmo is a new contributor. Be nice, and check out our Code of Conduct.
sekmo is a new contributor. Be nice, and check out our Code of Conduct.
sekmo is a new contributor. Be nice, and check out our Code of Conduct.
sekmo is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207607%2fpartition-array-to-hash-in-ruby%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown