Class: Hyde::PatternMatching::ReMatch
- Inherits:
-
Object
- Object
- Hyde::PatternMatching::ReMatch
- Defined in:
- lib/hyde/pattern_matching/rematch.rb
Overview
Note:
If you are planning to write your own pattern, this is the easiest one to read.
Regexp pattern matching wrapper. Following principles apply to the wrapper:
- Regexp input is canonicalized using Hyde::PatternMatching.canonicalize
- Matched content and anything before it is consumed in #match output
Class Method Summary collapse
-
.can_convert?(string) ⇒ Boolean
Test if input is convertible and if it should be converted.
Instance Method Summary collapse
-
#initialize(pattern) ⇒ ReMatch
constructor
A new instance of ReMatch.
-
#match(input) ⇒ Array(String,Array,Hash), FalseClass
Match the string and assign matches to parameters.
-
#match?(input) ⇒ Boolean
Test if a string can be matched.
Constructor Details
#initialize(pattern) ⇒ ReMatch
Returns a new instance of ReMatch.
13 14 15 |
# File 'lib/hyde/pattern_matching/rematch.rb', line 13 def initialize(pattern) @glob = pattern end |
Class Method Details
.can_convert?(string) ⇒ Boolean
Test if input is convertible and if it should be converted.
44 45 46 |
# File 'lib/hyde/pattern_matching/rematch.rb', line 44 def self.can_convert?(string) string.is_a? Regexp end |
Instance Method Details
#match(input) ⇒ Array(String,Array,Hash), FalseClass
Match the string and assign matches to parameters. Returns:
- Unmatched part of a string
- Unnamed parameters
- Named parameters
26 27 28 29 30 31 |
# File 'lib/hyde/pattern_matching/rematch.rb', line 26 def match(input) result = Hyde::PatternMatching.canonicalize(input).match(@glob) return false unless result [result.post_match, result.captures, result.named_captures] end |
#match?(input) ⇒ Boolean
Test if a string can be matched. Lighter version of match that doesn't assign any variables.
37 38 39 |
# File 'lib/hyde/pattern_matching/rematch.rb', line 37 def match?(input) Hyde::PatternMatching.canonicalize(input).match? @glob end |