Class: Arugula

Inherits:
Object
  • Object
show all
Defined in:
lib/arugula.rb,
lib/arugula/parts.rb,
lib/arugula/parser.rb,
lib/arugula/version.rb,
lib/arugula/match_data.rb

Defined Under Namespace

Modules: MatchAll, MatchAny, MatchNTimes, Wrapping Classes: AndPart, CapturePart, CharacterClassPart, DotPart, EOLPart, LiteralPart, MatchData, MetacharacterPart, NotPart, OrPart, Parser, Part, PlusPart, QuantifierPart, QuestionPart, RangePart, SOLPart, StarPart

Constant Summary collapse

VERSION =
'0.4.0'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ Arugula

Returns a new instance of Arugula.


10
11
12
# File 'lib/arugula.rb', line 10

def initialize(pattern)
  @root, @captures = Parser.new(pattern).parse!
end

Instance Attribute Details

#capturesObject (readonly)

Returns the value of attribute captures


5
6
7
# File 'lib/arugula.rb', line 5

def captures
  @captures
end

Instance Method Details

#==(other) ⇒ Object


44
45
46
47
# File 'lib/arugula.rb', line 44

def ==(other)
  return false unless other.is_a?(Arugula) || other.is_a?(Regexp)
  inspect == other.inspect
end

#hashObject


40
41
42
# File 'lib/arugula.rb', line 40

def hash
  to_s.hash
end

#match(str, index = 0) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/arugula.rb', line 19

def match(str, index = 0)
  match_data = MatchData.new(self, str)
  loop do
    match_data.reset_captures!
    match, end_index = @root.match(str, index, match_data)
    if match
      match_data.start_index = index
      match_data.end_index = end_index
      return match_data.freeze
    end
    index += 1
    return if index > str.size
  end
end

#match?(str, index = 0) ⇒ Boolean

Returns:

  • (Boolean)

14
15
16
17
# File 'lib/arugula.rb', line 14

def match?(str, index = 0)
  match_data = match(str, index)
  match_data && match_data.start_index
end

#to_sObject Also known as: inspect


34
35
36
# File 'lib/arugula.rb', line 34

def to_s
  "/#{@root}/"
end