Module: Utils::IRB::Regexp

Included in:
Regexp
Defined in:
lib/utils/irb.rb

Overview

A module that extends Regexp functionality with additional pattern matching and display capabilities.

Provides enhanced regexp operations including match highlighting and shell command integration.

Examples:

/pattern/ # => regular expression object
/pattern/.show_match("text") # => highlighted text match

Instance Method Summary collapse

Instance Method Details

#show_match(string, success: -> s { Term::ANSIColor.green { s } }, failure: -> s { Term::ANSIColor.red { s } }) ⇒ String

The show_match method evaluates a string against the receiver pattern and highlights matching portions.

This method tests whether the provided string matches the pattern represented by the receiver. When a match is found, it applies the success proc to highlight the matched portion of the string. If no match is found, it applies the failure proc to indicate that no match was found.

Parameters:

  • string (String)

    the string to be tested against the pattern

  • success (Proc) (defaults to: -> s { Term::ANSIColor.green { s } })

    a proc that processes the matched portion of the string

  • failure (Proc) (defaults to: -> s { Term::ANSIColor.red { s } })

    a proc that processes the “no match” indication

Returns:

  • (String)

    the formatted string with matched portions highlighted or a no match message



730
731
732
733
734
735
736
# File 'lib/utils/irb.rb', line 730

def show_match(
  string,
  success: -> s { Term::ANSIColor.green { s } },
  failure: -> s { Term::ANSIColor.red { s } }
)
  string =~ self ? "#{$`}#{success.($&)}#{$'}" : failure.("no match")
end