[ruby-core:124769] [Ruby Bug#21874] `PrettyPrint::SingleLine` is not API-compatible with `PrettyPrint`, breaking `PP` in some cases
Issue #21874 has been reported by trinistr (Alexander Bulancov). ---------------------------------------- Bug #21874: `PrettyPrint::SingleLine` is not API-compatible with `PrettyPrint`, breaking `PP` in some cases https://bugs.ruby-lang.org/issues/21874 * Author: trinistr (Alexander Bulancov) * Status: Open * ruby -v: ruby 4.0.1 (2026-01-13 revision e04267a14b) +PRISM [x86_64-linux] * Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN, 4.0: UNKNOWN ---------------------------------------- `PP` expects that a `#pretty_print` method can be used with both `PrettyPrint` and `PrettyPrint::SingleLine` interchangeably. This is used to [implement](https://github.com/ruby/pp/blob/b6f81ef83f9137c2300cb5eb96d440419c8f886f/lib...) `#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](https://github.com/ruby/pp/blob/b6f81ef83f9137c2300cb5eb96d440419c8f886f/lib...) 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](https://github.com/ruby/prettyprint/blob/c2ed83ca24f67a7ca6896d3e14c68e18a9f...). However, it is missing some less-used methods: `#fill_breakable`, `#group_sub` and also `#current_group`, `#break_outmost_groups` and `attr_reader`s. 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](https://github.com/trinistr/vector_number/blob/6968e3ea534dc21e5bb0f7d9cee2e...) works well for IRB and `pp`: ```ruby 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`: ```ruby 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. -- https://bugs.ruby-lang.org/
participants (1)
-
trinistr (Alexander Bulancov)