Module: Utils::IRB::Regexp

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

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



685
686
687
688
689
690
691
# File 'lib/utils/irb.rb', line 685

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