Reputation:
Starting form an array of hashes:
roles =[
{:id=>1, :name=>"alpha", :gid=>1},
{:id=>2, :name=>"beta", :gid=>2},
{:id=>3, :name=>"delta", :gid=>1},
{:id=>4, :name=>"epsilon", :gid=>1},
{:id=>5, :name=>"zeta", :gid=>3}
]
I am trying to get another structure:
groups = [
{:gid=>1, :roles=>[
{:id=>1, :name=>"alpha"},
{:id=>3, :name=>"delta"},
{:id=>4, :name=>"epsilon"}]},
{:gid=>2, :roles=>[{:id=>2, :name=>"beta"}]},
{:gid=>3, :roles=>[{:id=>5, :name=>"zeta"}]}
]
ANSWERS -- all answers output the same .. however I have to vote for the fastest one ..
time1 = Benchmark.measure do
(1..10000).each do
roles.group_by { |e| e.slice(:gid) }.map{
|k,v| k.merge(:roles => v.map { |e| e.except(:gid)}) }
end
end
puts time1
user system total real
1.150000 0.010000 1.160000 ( 1.165781)
time2 = Benchmark.measure do
(1..10000).each do
roles.group_by{|el| el[:gid]}.map{|gid, els|
{gid: gid, roles: els.map{ |el| { id: el[:id], name: el[:name]}}}}
end
end
puts time2
user system total real
0.270000 0.000000 0.270000 ( 0.278286)
time3 = Benchmark.measure do
(1..10000).each do
roles.group_by{|h| h.delete(:gid)}.map{|k, v| {gid: k, roles: v}}
end
end
puts time3
user system total real
0.130000 0.000000 0.130000 ( 0.134478)
Upvotes: 1
Views: 1486
Reputation: 110685
This uses the form of Hash#update (aka merge!
) that employs a block to determine the values of keys that are present in both hashes being merged. Here that block is:
{ |k,ov,nv| { :gid=>ov[:gid], :roles=>ov[:roles]+nv[:roles] } }
where k
is the common key, ov
is the value of the key in the being constructed and nv
is the value of the hash being merged in.
The calculation:
roles.each_with_object({}) do |g,h|
h.update(g[:gid]=>{ :gid=>g[:gid],
:roles=>[{:id=>g[:id], :name=>g[:name]}]
}
) { |k,ov,nv| { :gid=>ov[:gid], :roles=>ov[:roles]+nv[:roles] } }
end.values
#=> [{:gid=>1, :roles=>[{:id=>1, :name=>"alpha"},
# {:id=>3, :name=>"delta"},
# {:id=>4, :name=>"epsilon"}]},
# {:gid=>2, :roles=>[{:id=>2, :name=>"beta"}]},
# {:gid=>3, :roles=>[{:id=>5, :name=>"zeta"}]}]
Upvotes: 0
Reputation: 168101
An answer to your question is:
gid = nil
h = [
{:id=>1, :name=>"alpha", :gid=>1},
{:id=>3, :name=>"delta", :gid=>1},
{:id=>4, :name=>"epsilon", :gid=>1}
]
.each{|h| gid = h.delete(:gid)}
{gid: gid, roles: h}
but the whole thing can be done like this:
roles.group_by{|h| h.delete(:gid)}.map{|k, v| {gid: k, roles: v}}
Upvotes: 5
Reputation: 7685
A solution for Ruby 1.8.7 with ActiveSupport for methods slice()
and except()
.
require 'rubygems'
require 'active_support/all'
roles.group_by { |e| e.slice(:gid) }.
map { |k,v| k.merge(:roles => v.map { |e| e.except(:gid)}) }
#=> [{:gid=>1, :roles=>[{:name=>"alpha", :id=>1},
# {:name=>"delta", :id=>3},
# {:name=>"epsilon", :id=>4}]},
# {:gid=>2, :roles=>[{:name=>"beta", :id=>2}]},
# {:gid=>3, :roles=>[{:name=>"zeta", :id=>5}]}]
Upvotes: 0
Reputation: 83680
roles = [
{:id => 1, :name=>"alpha", :gid=>1},
{:id => 2, :name=>"beta", :gid=>2},
{:id => 3, :name=>"delta", :gid=>1},
{:id => 4, :name=>"epsilon", :gid=>1},
{:id => 5, :name=>"zeta", :gid=>3}
]
roles.group_by do |el|
el[:gid]
end.map do |gid, els|
{
gid: gid,
roles: els.map{ |el| { id: el[:id], name: el[:name] } }
}
end
Upvotes: 0