Dinesh Saini Dinesh Saini - 3 months ago 8
Ruby Question

Conversion of Ruby Hashes to Sort array of Hash

I have following input:

array = [{:year=>2015, :platform_id=>2},
{:year=>nil, :platform_id=>2},
{:year=>nil, :platform_id=>4},
{:year=>2015, :platform_id=>4}]


I need expected result to be:

[{platform_id=>2, year=>[2015, nil]},
{platform_id=>4, year=>[nil, 2015]}]


What I code is:

array.inject(:merge)


But that gets me this result, which is not what I want:

{:year=>2015, :platform_id=>4}


Updated below after answer received:

I do performance test after see the answers and here is the result:

arr = [
{:year => 2015, :platform_id => 2},
{:year => nil, :platform_id => 2},
{:year => nil, :platform_id => 4},
{:year => 2015, :platform_id => 4}
]

#approach 1
x1 = Time.now.to_f
exp = arr.each_with_object({}) do |h, exp|
exp[h[:platform_id]] ||= {:platform_id => h[:platform_id], :year => []}
exp[h[:platform_id]][:year] << h[:year]
end.values
x2 = Time.now.to_f
p x2-x1

#approach 2
x3 = Time.now.to_f
new_data = arr.group_by { |d| d[:platform_id] }
new_arr = []
new_data.each do |k,v|
t2 = v.map{|x| x[:year]}
temp = {"platform_id": k, "years": t2}
new_arr.push(temp)
end
x4 = Time.now.to_f
p x4-x3

#approach 3
x5 = Time.now.to_f
f = arr.each_with_object({}) { |g,h|
h.update(g[:platform_id]=>[g[:year]]) { |_,o,n| o+n } }
#=> {2=>[2015, nil], 4=>[nil, 2015]}

f.map { |k,v| { :platform_id=>k, :year=>v } }
x6 = Time.now.to_f
p x6-x5

#output is:
9.059906005859375e-06
6.4373016357421875e-06
9.775161743164062e-06

Answer
arr = [
  { :year=>2015, :platform_id=>2 }, 
  { :year=>nil,  :platform_id=>2 },
  { :year=>nil,  :platform_id=>4 },
  { :year=>2015, :platform_id=>4 }
]

arr.each_with_object({}) { |g,h|
  h.update(g[:platform_id]=>[g[:year]]) { |_,o,n| o+n } }.
    map { |k,v| { :platform_id=>k, :year=>v } }
    #=> [{:platform_id=>2, :year=>[2015, nil]},
    #    {:platform_id=>4, :year=>[nil, 2015]}] 

The two steps are as follows.

f = arr.each_with_object({}) { |g,h|
  h.update(g[:platform_id]=>[g[:year]]) { |_,o,n| o+n } }
  #=> {2=>[2015, nil], 4=>[nil, 2015]} 

f.map { |k,v| { :platform_id=>k, :year=>v } }
  #=> [{:platform_id=>2, :year=>[2015, nil]},
  #    {:platform_id=>4, :year=>[nil, 2015]}] 

The first step uses the form of Hash#update (aka merge!) that employs a block (here { |_,o,n| o+n }) that computes the value of keys that are present in both hashes being merged.