Module: Samsao::Regexp
- Defined in:
- lib/samsao/regexp.rb
Overview
Regexp utility methods
Class Method Summary collapse
-
.from_matcher(matcher, when_string_pattern_prefix_with: '') ⇒ Regexp
Turns an input into a Regexp.
-
.from_source(source) ⇒ Regexp
Turns a source entry input into a Regexp.
Class Method Details
.from_matcher(matcher, when_string_pattern_prefix_with: '') ⇒ Regexp
Turns an input into a Regexp. If the input is a String, it turns it into a Regexp and escape all special characters. If the input is already a Regexp, it returns it without modification.
23 24 25 26 27 |
# File 'lib/samsao/regexp.rb', line 23 def self.from_matcher(matcher, when_string_pattern_prefix_with: '') return matcher if matcher.is_a?(::Regexp) /#{when_string_pattern_prefix_with}#{::Regexp.quote(matcher)}/ end |
.from_source(source) ⇒ Regexp
Turns a source entry input into a Regexp. Uses rules from [from_matcher](#from_matcher) function and append a ‘^` to final regexp when the source if a pure String.
10 11 12 |
# File 'lib/samsao/regexp.rb', line 10 def self.from_source(source) from_matcher(source, when_string_pattern_prefix_with: '^') end |