Skip to content
Open
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
6 changes: 5 additions & 1 deletion lib/pp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def PP.width_for(out)
_, width = out.winsize
rescue LoadError, NoMethodError, SystemCallError
end
# winsize can report 0 for a non-tty or constrained device; treat that
# like a missing width so we fall back to COLUMNS / 80 instead of -1.
width = nil unless width&.nonzero?
(width || ENV['COLUMNS']&.to_i&.nonzero? || 80) - 1
end

Expand Down Expand Up @@ -220,7 +223,8 @@ def pp(obj)
#
# group(1, '#<' + obj.class.name, '>') { ... }
def object_group(obj, &block) # :yield:
group(1, '#<' + obj.class.name, '>', &block)
# Anonymous classes have a nil #name; #to_s still yields a usable label.
group(1, '#<' + (obj.class.name || obj.class.to_s), '>', &block)
end

# A convenience method, like object_group, but also reformats the Object's
Expand Down
14 changes: 14 additions & 0 deletions test/test_pp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ def test_range
assert_equal("false..false\n", PP.pp(false..false, "".dup))
assert_equal("nil..nil\n", PP.pp(nil..nil, "".dup))
end

def test_width_for_zero_winsize
out = Object.new
def out.winsize; [24, 0]; end
assert_equal(79, PP.width_for(out))
end

def test_object_group_anonymous_class
obj = Class.new.new
q = PP.new("".dup)
q.object_group(obj) { q.text "x" }
q.flush
assert_match(/\A#<#<Class:0x[0-9a-f]+>x>\z/, q.output)
end
end

class HasInspect
Expand Down
Loading