From: "mame (Yusuke Endoh)" Date: 2021-11-24T05:06:43+00:00 Subject: [ruby-core:106239] [Ruby master Feature#6733] New inspect framework Issue #6733 has been updated by mame (Yusuke Endoh). Eregon (Benoit Daloze) wrote in #note-8: > Should the (remaining) limit (of nb of characters) be passed to `inspect_to`? > Otherwise if the logic is like creating a long string, and then appending to the buffer, we have the problem that it still takes a long time and might waste a lot of needless computation. I don't think that your concern will be addressed even if limit is passed. The logic can still create a long string and then truncate it to fit with the limit. `inspect_to` should append small strings to the buffer, which should be noted as a best practice. Dan0042 (Daniel DeLorme) wrote in #note-9: > This sounds like a very powerful and flexible approach, but perhaps a little over-engineered? > Maybe a more KISS approach would be sufficient; just define `Object#simple_inspect` to be used when we need a short string, like in `NoMethodError#message`. Indeed this proposal was unearthed in the context of `NoMethodError#message` (#18285), but the use case is not limited to that. ---------------------------------------- Feature #6733: New inspect framework https://bugs.ruby-lang.org/issues/6733#change-94859 * Author: akr (Akira Tanaka) * Status: Open * Priority: Normal ---------------------------------------- After we discussed http://bugs.ruby-lang.org/issues/6291 at a developer meeting, we re-realized new inspect framework may be useful. Problem: * inspect method may generate too long string but sometimes whole string is not required. For example, first 70 characters are enough for error messages (backtrace). * inspect can't know a encoding to be expected. * inspect generates may short strings and discard them immediately. If we have a new method, inspect_to(buffer), and it (or overridden method in subclass) adds the inspected result to buffer, we can solve above problems. buffer has a method, <<. It may be a string, IO or other object. For too long string, buffer itself can throw (or raise) when buffered output is reached to a specified limit. For encoding, buffer can record an encoding. (p method creates a buffer object using $stdout's encoding.) For small strings, in C level, we can create a rb_buffer_add(VALUE buffer, const char *p, long len) and it don't need to allocate a String object. This is big change but we can preserve compatibility by following default inspect_to method: class Object def inspect_to(buffer) buffer << self.inspect end end If legacy class which has inspect but not inspect_to, Object#inspect_to calls inspect and use the result. -- https://bugs.ruby-lang.org/ Unsubscribe: