Issue #22443 has been reported by bkuhlmann (Brooke Kuhlmann). ---------------------------------------- Feature #22443: Remove multi-line regular expression comments from object inspection https://bugs.ruby-lang.org/issues/22443 * Author: bkuhlmann (Brooke Kuhlmann) * Status: Open ---------------------------------------- ## Why Hello. 👋 Would it be possible to remove multi-line regular expression comments from object inspection? For example, this is nice for readability via the source code: ``` ruby class Demo PATTERN = / (?: # Start non-capture group. (?<position>[0-9]+) # Position capture group, numbers only. - # Dash. )? # End non-capture group and make it optional. (?<name>[[:word:]-]+) # Name capture group, required. (?: # Non-capture group start. \+ # Plus. (?<meta>[[:word:]\-,=]+) # Meta capture group, required. )? # End non-capture group and make it optional. /x def initialize pattern = PATTERN @pattern = pattern end private attr_reader :pattern end ``` However, the above is quite noisy when inspecting the object. Example: ``` ruby Demo.new #<Demo:0x000000012fed5730 @pattern= / (?: # Start non-capture group. (?<position>[0-9]+) # Position capture group, numbers only. - # Dash. )? # End non-capture group and make it optional. (?<name>[[:word:]-]+) # Name capture group, required. (?: # Non-capture group start. \+ # Plus. (?<meta>[[:word:]\-,=]+) # Meta capture group, required. )? # End non-capture group and make it optional. /x> ``` ## How It would be nice to automatically strip the comments and collapse the newlines for the object when inspecting it. Example: ``` ruby Demo.new #<Demo:0x0000000131d588d8 @pattern=/(?:(?<position>[0-9]+)-)?(?<name>[[:word:]-]+)(?:\+(?<meta>[[:word:]\-,=]+))?/> ``` The above frees up a lot of vertical space -- especially when inspecting multiple objects at once -- so you don't have to scroll so much via the command line. You still get a visual indication that a pattern is used and can jump to the source if you need readability to learn more. ## Notes One solution to the above is to add this private method: ``` def instance_variables_to_inspect = %i[@pattern] ``` The above definitely reduces the verbosity but at the cost of hiding the instance variable. -- https://bugs.ruby-lang.org/