Class: Hyde::PatternMatching::ReMatch

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • input

Returns:

  • (Boolean)

    Input can be safely converted to Glob



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

Parameters:

  • input (String)

    String to match

Returns:

  • (Array(String,Array,Hash))
  • (FalseClass)

    if input doesn't match regexp



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.

Parameters:

  • input (String)

    String to match

Returns:

  • (Boolean)


37
38
39
# File 'lib/hyde/pattern_matching/rematch.rb', line 37

def match?(input)
  Hyde::PatternMatching.canonicalize(input).match? @glob
end