Module: Hash::PrettyInspect

Included in:
Hash
Defined in:
lib/pretty_debug.rb

Constant Summary collapse

SingleLength =
50
KeyLengthMax =
30

Instance Method Summary collapse

Instance Method Details

#inspectObject



257
258
259
260
261
262
263
264
265
266
# File 'lib/pretty_debug.rb', line 257

def inspect
  return super if recursive?
  keys = keys().map(&:inspect)
  #Purpose# When `self == empty?`, `...max` becomes `nil`. `to_i` turns it to `0`.
  w = keys.map(&:length).max.to_i
  w = w > KeyLengthMax ? KeyLengthMax : w
  s = [keys, values].transpose.map{|k, v| "#{k.ljust(w)} => #{v.inspect}"}
  length < 2 || s.map(&:length).inject(:+) < SingleLength ?
    "{#{s.join(", ".freeze)}}" :  "{#$/#{s.join(",#$/".freeze).indent}#$/}"
end