* [ruby-core:124769] [Ruby Bug#21874] `PrettyPrint::SingleLine` is not API-compatible with `PrettyPrint`, breaking `PP` in some cases
@ 2026-02-11 12:00 trinistr (Alexander Bulancov) via ruby-core
0 siblings, 0 replies; only message in thread
From: trinistr (Alexander Bulancov) via ruby-core @ 2026-02-11 12:00 UTC (permalink / raw)
To: ruby-core; +Cc: trinistr (Alexander Bulancov)
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/pp.rb#L402) `#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/pp.rb#L214) 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/c2ed83ca24f67a7ca6896d3e14c68e18a9f24f6a/lib/prettyprint.rb#L528). 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/6968e3ea534dc21e5bb0f7d9cee2ee7bb43e96be/lib/vector_number/stringifying.rb#L79) 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/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-02-11 12:03 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-11 12:00 [ruby-core:124769] [Ruby Bug#21874] `PrettyPrint::SingleLine` is not API-compatible with `PrettyPrint`, breaking `PP` in some cases trinistr (Alexander Bulancov) via ruby-core
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).