2
\$\begingroup\$

I am working on a Ruby gem to pull data from the Wikidata API. The data is deeply nested, as it contains a lot of repetition.

I have written a piece of Ruby (sans Rails) code that enables an array of hashes to be merged together, with any duplicate key-value pairs being stored as an array under the same key.

For example:

[{:a=>1, :b=>2}, {:b=>3, :c=>4}, {:d=>5, :a=>6}].merge_hashes

gives:

{:a=>[1, 6], :b=>[2, 3], :c=>4, :d=>5}

I began with the following:

class Array
 def merge_hashes
 raise StandardError.new "Array is not an Array of Hashes" unless self.all? {|e| e.is_a? Hash}
 self.each_with_object({}) do |el, h|
 el.each do |k, v|
 if h[k].nil?
 h[k] = v
 else
 h[k] = Array.new([h[k]]) << v
 h[k] = h[k].flatten
 end
 end
 end
 end
end

And refactored to:

class Array
def merge_hashes
 raise StandardError.new "Array is not an Array of Hashes" unless self.all? {|e| e.is_a? Hash}
 self.each_with_object({}) { |el, h| el.each { |k, v| h[k].nil? ? h[k] = v : h[k] = (Array.new([h[k]]) << v).flatten } }
 end 
end

Are there any other improvements I could make? Is that an acceptable exception to throw?

asked May 29, 2016 at 21:09
\$\endgroup\$

2 Answers 2

5
\$\begingroup\$

I think the refactor is barely readable, the first version is better. Anyway, if you use functional abstractions, instead of doing a imperative processing from scratch, algorithms tend to be more clear.

In this case, we can use the abstraction Enumerable#map_by (a group_by variation where you can control both the grouping function and the accumulated values) and write:

require 'facets'
class Array
 def merge_hashes
 flat_map(&:to_a).map_by { |k, v| [k, v] }
 end 
end

Note that this snippet always returns an array as a value, instead of the scalar/list you have.

answered May 29, 2016 at 22:47
\$\endgroup\$
2
  • \$\begingroup\$ Thanks for the answer and for the link to facets; I didn't know about it. \$\endgroup\$ Commented May 30, 2016 at 6:22
  • 1
    \$\begingroup\$ Huh, I see your point about the mixed data types. Thanks for the good advice. \$\endgroup\$ Commented Jun 1, 2016 at 10:16
1
\$\begingroup\$

I've tried to simplify assignment condition (splat operator is used instead of #flatten):

class Array
 def merge_hashes_alt
 # ...
 self.each_with_object({}) do |el, h|
 el.each { |k, v| h[k] = h[k] ? [*h[k]] << v : v }
 end
 end
end
answered May 30, 2016 at 5:50
\$\endgroup\$
1
  • \$\begingroup\$ I think this is good, because it doesn't check for nil? explicitly and only has one h[k] = assignment. In addition, it gets rid of the Array.new() and the flatten, as you mentioned. \$\endgroup\$ Commented May 30, 2016 at 6:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.