Class: Hyde::Pattern
- Inherits:
-
Object
- Object
- Hyde::Pattern
- Defined in:
- lib/hyde/pattern_matching.rb
Overview
Delegate class for all available patterns. Picks appropriate pattern based on contents.
Instance Method Summary collapse
-
#initialize(pattern) ⇒ Pattern
constructor
A new instance of Pattern.
-
#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.
-
#static? ⇒ Boolean
Checks if pattern object is static (is a simple String pattern).
Constructor Details
#initialize(pattern) ⇒ Pattern
Returns a new instance of Pattern.
17 18 19 20 |
# File 'lib/hyde/pattern_matching.rb', line 17 def initialize(pattern) @pattern = patternify(pattern) @static = @pattern.is_a? String 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
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/hyde/pattern_matching.rb', line 36 def match(input) if @pattern.is_a? String input = Hyde::PatternMatching.canonicalize(input) if input.start_with?(@pattern) [input.delete_prefix(@pattern), [], {}] else false end else @pattern.match(input) end end |
#match?(input) ⇒ Boolean
Test if a string can be matched. Lighter version of match that doesn’t assign any variables.
53 54 55 56 57 58 59 |
# File 'lib/hyde/pattern_matching.rb', line 53 def match?(input) if @pattern.is_a? String Hyde::PatternMatching.canonicalize(input).start_with? pattern else @pattern.match?(input) end end |
#static? ⇒ Boolean
Checks if pattern object is static (is a simple String pattern).
23 24 25 |
# File 'lib/hyde/pattern_matching.rb', line 23 def static? @static end |