Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/pp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def PP.width_for(out)
#
# PP.pp returns +out+.
def PP.pp(obj, out=$>, width=width_for(out))
q = PP.new(out, width)
q = new(out, width)
q.guard_inspect_key {q.pp obj}
q.flush
#$pp = q
Expand Down Expand Up @@ -286,16 +286,21 @@ def pp_hash(obj)
group(1, '{', '}') {
seplist(obj, nil, :each_pair) {|k, v|
group {
pp k
text '=>'
group(1) {
breakable ''
pp v
}
pp_hash_pair k, v
}
}
}
end

# A pretty print for a pair of Hash
def pp_hash_pair(k, v)
pp k
text '=>'
group(1) {
breakable ''
pp v
}
end
end

include PPMethods
Expand Down
25 changes: 25 additions & 0 deletions test/test_pp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,29 @@ def test_lasgn_literal
end
end

class PPInheritedTest < Test::Unit::TestCase
class PPSymbolHash < PP
def pp_hash_pair(k, v)
case k
when Symbol
text k.inspect.delete_prefix(":")
text ":"
group(1) {
breakable
pp v
}
else
super
end
end
end

def test_hash_override
obj = {k: 1, "": :null, "0": :zero, 100 => :ten}
assert_equal <<~EXPECT, PPSymbolHash.pp(obj, "".dup)
{k: 1, "": :null, "0": :zero, 100=>:ten}
EXPECT
end
end

end