module PrettyHash
# Usage: PrettyHash.call(nested_hash)
# Prints the nested hash in the easy to look on format
# Returns the amount of all values in the nested hash
def self.call(hash, level: 0, indent: 2)
unique_values_count = 0
hash.each do |k, v|
(level * indent).times { print ' ' }
print "#{k}:"
if v.is_a?(Hash)
puts
unique_values_count += call(v, level: level + 1, indent: indent)
else
puts " #{v}"
unique_values_count += 1
end
end
unique_values_count
end
end
使用示例:
h = {a: { b: { c: :d }, e: :f }, g: :i }
PrettyHash.call(h)
a:
b:
c: d
e: f
g: i
=> 3