Skip to content

Commit 507eebf

Browse files
authored
Update pp for Set to use new inspect format (#43)
Ruby 3.5 will use `Set[1, 2, 3]`. This updates pp to use the same format.
1 parent 24a0d3f commit 507eebf

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

lib/pp.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -452,18 +452,16 @@ def pretty_print_cycle(q) # :nodoc:
452452
end
453453
class Set # :nodoc:
454454
def pretty_print(pp) # :nodoc:
455-
pp.group(1, '#<Set:', '>') {
456-
pp.breakable
457-
pp.group(1, '{', '}') {
458-
pp.seplist(self) { |o|
459-
pp.pp o
460-
}
455+
pp.group(1, "#{self.class.name}[", ']') {
456+
pp.seplist(self) { |o|
457+
pp.pp o
461458
}
462459
}
463460
end
464461

465462
def pretty_print_cycle(pp) # :nodoc:
466-
pp.text sprintf('#<Set: {%s}>', empty? ? '' : '...')
463+
name = self.class.name
464+
pp.text(empty? ? "#{name}[]" : "#{name}[...]")
467465
end
468466
end if set_pp
469467

test/test_pp.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
require 'pp'
44
require 'delegate'
5+
require 'set'
56
require 'test/unit'
67
require 'ruby2_keywords'
78

89
module PPTestModule
910

11+
SetPP = Set.instance_method(:pretty_print).source_location[0].end_with?("/pp.rb")
12+
1013
class PPTest < Test::Unit::TestCase
1114
def test_list0123_12
1215
assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], ''.dup, 12))
@@ -16,6 +19,10 @@ def test_list0123_11
1619
assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], ''.dup, 11))
1720
end
1821

22+
def test_set
23+
assert_equal("Set[0, 1, 2, 3]\n", PP.pp(Set[0,1,2,3], ''.dup, 16))
24+
end if SetPP
25+
1926
OverriddenStruct = Struct.new("OverriddenStruct", :members, :class)
2027
def test_struct_override_members # [ruby-core:7865]
2128
a = OverriddenStruct.new(1,2)
@@ -164,6 +171,12 @@ def test_hash
164171
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
165172
end
166173

174+
def test_set
175+
s = Set[]
176+
s.add s
177+
assert_equal("Set[Set[...]]\n", PP.pp(s, ''.dup))
178+
end if SetPP
179+
167180
S = Struct.new("S", :a, :b)
168181
def test_struct
169182
a = S.new(1,2)

0 commit comments

Comments
 (0)