Class: Hash

Inherits:
Object show all
Defined in:
lib/pretty_debug.rb,
lib/pretty_debug.rb

Constant Summary collapse

KeyLengthMax =
30

Instance Method Summary collapse

Instance Method Details

#compatible?(other) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
# File 'lib/pretty_debug.rb', line 38

def compatible? other
	other.case?(Hash) and
	other.size == size and
	other.keys.all?{|k| key?(k)}
end

#inspectObject



176
177
178
179
180
181
182
# File 'lib/pretty_debug.rb', line 176

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

#ltsv(*args) ⇒ Object



43
44
45
46
47
48
# File 'lib/pretty_debug.rb', line 43

def ltsv *args
	if args.empty? then
		map{|k, v| "#{k}:#{v.to_s.tr("\t", " ")}"} else
		args.map{|k| "#{k}:#{self[k].to_s.tr("\t", " ")}"}
	end.join("\t")
end