diff options
| author | Jeremy Evans <code@jeremyevans.net> | 2025-10-05 01:06:07 +0900 |
|---|---|---|
| committer | Nobuyoshi Nakada <nobu@ruby-lang.org> | 2025-10-05 15:13:56 +0900 |
| commit | ec1655d52019e66725efe94b5bc88b3a98af284e (patch) | |
| tree | b9c04577fba3b8200c9843a58e69de5739208edb | |
| parent | 4ddbee33097c431e83d4dbc6a130349067bf7c0a (diff) | |
[ruby/pp] Update pp for Set to use new inspect format
(https://github.com/ruby/pp/pull/43)
Ruby 3.5 will use `Set[1, 2, 3]`. This updates pp to use the same format.
https://github.com/ruby/pp/commit/507eebf711
| -rw-r--r-- | lib/pp.rb | 12 | ||||
| -rw-r--r-- | test/test_pp.rb | 13 |
2 files changed, 18 insertions, 7 deletions
@@ -452,18 +452,16 @@ if defined?(Set) end class Set # :nodoc: def pretty_print(pp) # :nodoc: - pp.group(1, '#<Set:', '>') { - pp.breakable - pp.group(1, '{', '}') { - pp.seplist(self) { |o| - pp.pp o - } + pp.group(1, "#{self.class.name}[", ']') { + pp.seplist(self) { |o| + pp.pp o } } end def pretty_print_cycle(pp) # :nodoc: - pp.text sprintf('#<Set: {%s}>', empty? ? '' : '...') + name = self.class.name + pp.text(empty? ? "#{name}[]" : "#{name}[...]") end end if set_pp diff --git a/test/test_pp.rb b/test/test_pp.rb index 28da00e3e7..c5340a3e65 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -2,11 +2,14 @@ require 'pp' require 'delegate' +require 'set' require 'test/unit' require 'ruby2_keywords' module PPTestModule +SetPP = Set.instance_method(:pretty_print).source_location[0].end_with?("/pp.rb") + class PPTest < Test::Unit::TestCase def test_list0123_12 assert_equal("[0, 1, 2, 3]\n", PP.pp([0,1,2,3], ''.dup, 12)) @@ -16,6 +19,10 @@ class PPTest < Test::Unit::TestCase assert_equal("[0,\n 1,\n 2,\n 3]\n", PP.pp([0,1,2,3], ''.dup, 11)) end + def test_set + assert_equal("Set[0, 1, 2, 3]\n", PP.pp(Set[0,1,2,3], ''.dup, 16)) + end if SetPP + OverriddenStruct = Struct.new("OverriddenStruct", :members, :class) def test_struct_override_members # [ruby-core:7865] a = OverriddenStruct.new(1,2) @@ -164,6 +171,12 @@ class PPCycleTest < Test::Unit::TestCase assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup)) end + def test_set + s = Set[] + s.add s + assert_equal("Set[Set[...]]\n", PP.pp(s, ''.dup)) + end if SetPP + S = Struct.new("S", :a, :b) def test_struct a = S.new(1,2) |
