class HArray
Public Instance Methods
count_by() { |x| ... }
click to toggle source
# File lib/trials/utils/h_array.rb, line 6 def count_by hashes .group_by { |x| yield(x) } .map { |k, v| [k, v.length] } .to_h end
hashes()
click to toggle source
# File lib/trials/utils/h_array.rb, line 2 def hashes self end
merge(*groups, key:, join: :inner)
click to toggle source
# File lib/trials/utils/h_array.rb, line 35 def merge(*groups, key:, join: :inner) groups = [self, *groups].map do |group| group .map { |h| [h.dig(key), h] } .to_h end keys = begin case join when :inner groups.map(&:keys).reduce(&:&) when :all groups.flat_map(&:keys).uniq when :first groups.first.keys end end keys.map do |key| groups .map { |g| g.dig(key) } .compact .reduce(&:merge) end end
normalize_keys()
click to toggle source
# File lib/trials/utils/h_array.rb, line 23 def normalize_keys hashes.map do |h| h .map { |k, v| [normalize_key(k), v] } .to_h end end
rename_keys(**renames)
click to toggle source
# File lib/trials/utils/h_array.rb, line 13 def rename_keys(**renames) renames.each do |orig_key, new_key| hashes.map do |h| new_h = h.dup new_h[new_key] = new_h.delete(orig_key) new_h end end end
uniq_keys()
click to toggle source
# File lib/trials/utils/h_array.rb, line 31 def uniq_keys hashes.flat_map(&:keys).uniq.compact end
Private Instance Methods
normalize_key(k)
click to toggle source
# File lib/trials/utils/h_array.rb, line 63 def normalize_key(k) k.downcase.to_sym end