Skip to content

Commit

Permalink
[Feature #19045] Add support Data#pretty_print
Browse files Browse the repository at this point in the history
  • Loading branch information
osyo-manga authored and nobu committed Oct 14, 2022
1 parent f7a7944 commit 343a20d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/pp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,26 @@ def pretty_print_cycle(q) # :nodoc:
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
Expand Down
9 changes: 9 additions & 0 deletions test/test_pp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ def test_struct
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}
Expand Down

0 comments on commit 343a20d

Please sign in to comment.