Bug #21874
closed`PrettyPrint::SingleLine` is not API-compatible with `PrettyPrint`, breaking `PP` in some cases
Description
PP expects that a #pretty_print method can be used with both PrettyPrint and PrettyPrint::SingleLine interchangeably. This is used to implement #pretty_print_inspect. As can be seen from PP#pp, a NoMethodError is expected when an object does not have #pretty_print, so it is replaced with Kernel#inspect.
As far as I can tell, PrettyPrint::SingleLine is intended to be compatible with regular PrettyPrint, considering it has no-op methods and arguments. However, it is missing some less-used methods: #fill_breakable, #group_sub and also #current_group, #break_outmost_groups and attr_readers. I think only the first two are intended to be used by pretty-printed objects themselves, other methods are for use by output buffers a,d thus expected to be mssing.
I'm personally interested in using #fill_breakable, because #breakable creates too many unneeded newlines in my case. Implementing #pretty_print for my class works well for IRB and pp:
irb(main):001> v = VectorNumber[1, "a", "z"]
=> (1 + 1⋅"a" + 1⋅"z")
irb(main):002> pp v
(1 + 1⋅"a" + 1⋅"z")
=> (1 + 1⋅"a" + 1⋅"z")
But #pretty_print_inspect inserts stringified instance when encountering fill_breakable due to NoMethodError:
irb(main):003> v.pretty_print_inspect
=> "(1#<VectorNumber:0x00007fcdd5bb10c0 ...>"
Implementing #fill_breakable and #group_sub for PrettyPrint::SingleLine is trivial, I can make a PR if needed.
Updated by Anonymous 3 days ago
- Status changed from Open to Closed
Applied in changeset git|8f7c12830f431a92582a1a7831dee248820b27d9.
[ruby/prettyprint] [Bug #21874] pretty print single line compatibility