[ruby/pp] [Feature #19045] Add support Data#pretty_print

https://github.com/ruby/pp/commit/343a20d721
This commit is contained in:
manga_osyo 2022-10-10 12:30:35 +09:00 коммит произвёл git
Родитель 5ccb625fbb
Коммит 7b7e5153e8
2 изменённых файлов: 29 добавлений и 0 удалений

Просмотреть файл

@ -416,6 +416,26 @@ class Struct # :nodoc:
end end
end end
class Data # :nodoc:
def pretty_print(q) # :nodoc:
q.group(1, sprintf("#<data %s", PP.mcall(self, Kernel, :class).name), '>') {
q.seplist(PP.mcall(self, Data, :members), lambda { q.text "," }) {|member|
q.breakable
q.text member.to_s
q.text '='
q.group(1) {
q.breakable ''
q.pp public_send(member)
}
}
}
end
def pretty_print_cycle(q) # :nodoc:
q.text sprintf("#<data %s:...>", PP.mcall(self, Kernel, :class).name)
end
end if "3.2" <= RUBY_VERSION
class Range # :nodoc: class Range # :nodoc:
def pretty_print(q) # :nodoc: def pretty_print(q) # :nodoc:
q.pp self.begin q.pp self.begin

Просмотреть файл

@ -152,6 +152,15 @@ class PPCycleTest < Test::Unit::TestCase
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup)) assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
end end
if "3.2" <= RUBY_VERSION
D = Data.define(:aaa, :bbb)
def test_data
a = D.new("aaa", "bbb")
assert_equal("#<data PPTestModule::PPCycleTest::D\n aaa=\"aaa\",\n bbb=\"bbb\">\n", PP.pp(a, ''.dup, 20))
assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup))
end
end
def test_object def test_object
a = Object.new a = Object.new
a.instance_eval {@a = a} a.instance_eval {@a = a}