Class: Matchi::Match
- Inherits:
-
Object
- Object
- Matchi::Match
- Defined in:
- lib/matchi/match.rb
Overview
Pattern matching matcher that checks if a value matches a regular expression.
This matcher verifies that a value matches a pattern using Ruby’s Regexp#match? method. It’s particularly useful for string validation, pattern matching, and text analysis. The matcher ensures secure pattern matching by requiring the pattern to respond to match?.
Instance Method Summary collapse
-
#initialize(expected) ⇒ Match
constructor
Initialize the matcher with a pattern.
-
#match? { ... } ⇒ Boolean
Checks if the yielded value matches the expected pattern.
-
#to_s ⇒ String
Returns a human-readable description of the matcher.
Constructor Details
#initialize(expected) ⇒ Match
Initialize the matcher with a pattern.
42 43 44 45 46 |
# File 'lib/matchi/match.rb', line 42 def initialize(expected) raise ::ArgumentError, "expected must respond to match?" unless expected.respond_to?(:match?) @expected = expected end |
Instance Method Details
#match? { ... } ⇒ Boolean
Checks if the yielded value matches the expected pattern.
This method uses the pattern’s match? method to perform the comparison. The match is performed on the entire string unless the pattern specifically allows partial matches.
67 68 69 70 71 |
# File 'lib/matchi/match.rb', line 67 def match? raise ::ArgumentError, "a block must be provided" unless block_given? @expected.match?(yield) end |
#to_s ⇒ String
Returns a human-readable description of the matcher.
81 82 83 |
# File 'lib/matchi/match.rb', line 81 def to_s "match #{@expected.inspect}" end |