Module: HashHelper

Defined in:
app/helpers/hash_helper.rb

Instance Method Summary collapse

Instance Method Details

#pretty_hash(hash, nesting = 0) ⇒ Object


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/helpers/hash_helper.rb', line 3

def pretty_hash(hash, nesting = 0)
  return '{}' if hash.empty?

  tab_size = 2
  nesting += 1

  pretty  = "{"
  sorted_keys = hash.keys.sort
  sorted_keys.each do |key|
    val = hash[key].is_a?(Hash) ? pretty_hash(hash[key], nesting) : hash[key].inspect
    pretty += "\n#{' '*nesting*tab_size}"
    pretty += "#{key.inspect} => #{val}"
    pretty += "," unless key == sorted_keys.last

  end
  nesting -= 1
  pretty += "\n#{' '*nesting*tab_size}}"
end