ruby-changes:73984
From: manga_osyo <ko1@a...>
Date: Fri, 14 Oct 2022 21:31:37 +0900 (JST)
Subject: [ruby-changes:73984] 7b7e5153e8 (master): [ruby/pp] [Feature #19045] Add support Data#pretty_print
https://git.ruby-lang.org/ruby.git/commit/?id=7b7e5153e8 From 7b7e5153e81288fe57ae64f2e1db228435156aeb Mon Sep 17 00:00:00 2001 From: manga_osyo <manga.osyo@g...> Date: Mon, 10 Oct 2022 12:30:35 +0900 Subject: [ruby/pp] [Feature #19045] Add support Data#pretty_print https://github.com/ruby/pp/commit/343a20d721 --- lib/pp.rb | 20 ++++++++++++++++++++ test/test_pp.rb | 9 +++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/pp.rb b/lib/pp.rb index f43356a3df..81551aa116 100644 --- a/lib/pp.rb +++ b/lib/pp.rb @@ -416,6 +416,26 @@ class Struct # :nodoc: https://github.com/ruby/ruby/blob/trunk/lib/pp.rb#L416 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: def pretty_print(q) # :nodoc: q.pp self.begin diff --git a/test/test_pp.rb b/test/test_pp.rb index 9cef555d79..4fcb8df4b0 100644 --- a/test/test_pp.rb +++ b/test/test_pp.rb @@ -152,6 +152,15 @@ class PPCycleTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_pp.rb#L152 assert_equal("#{a.inspect}\n", PP.pp(a, ''.dup)) 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 a = Object.new a.instance_eval {@a = a} -- cgit v1.2.1 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/