Skip to content

Commit f204df3

Browse files
committed
Get rid of hardcoded class name
So that the `pp` method can work in inherited classes with that class.
1 parent f5cee21 commit f204df3

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/pp.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def PP.width_for(out)
9393
#
9494
# PP.pp returns +out+.
9595
def PP.pp(obj, out=$>, width=width_for(out))
96-
q = PP.new(out, width)
96+
q = new(out, width)
9797
q.guard_inspect_key {q.pp obj}
9898
q.flush
9999
#$pp = q

test/test_pp.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,36 @@ def test_lasgn_literal
245245
end
246246
end
247247

248+
class PPInheritedTest < Test::Unit::TestCase
249+
class PPSymbolHash < PP
250+
def pp_hash(obj)
251+
group(1, "{", "}") {
252+
seplist(obj, nil, :each_pair) {|k, v|
253+
case k
254+
when Symbol
255+
text k.inspect.delete_prefix(":")
256+
text ":"
257+
sep = " "
258+
else
259+
pp k
260+
text "=>"
261+
sep = ""
262+
end
263+
group(1) {
264+
breakable sep
265+
pp v
266+
}
267+
}
268+
}
269+
end
270+
end
271+
272+
def test_hash_override
273+
obj = {k: 1, "": :null, "0": :zero, 100 => :ten}
274+
assert_equal <<~EXPECT, PPSymbolHash.pp(obj, "".dup)
275+
{k: 1, "": :null, "0": :zero, 100=>:ten}
276+
EXPECT
277+
end
278+
end
279+
248280
end

0 commit comments

Comments
 (0)