Project

General

Profile

Actions

Feature #22443

open

Remove multi-line regular expression comments from object inspection

Feature #22443: Remove multi-line regular expression comments from object inspection

Added by bkuhlmann (Brooke Kuhlmann) about 6 hours ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:127112]

Description

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:

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:

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:

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.

No data to display

Actions

Also available in: PDF Atom