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, Wrapping Classes: AndPart, CapturePart, CharacterClassPart, DotPart, EOLPart, LiteralPart, MatchData, MetacharacterPart, OrPart, Parser, Part, PlusPart, RangePart, SOLPart, StarPart

Constant Summary collapse

VERSION =
'0.3.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

#match(str, index = 0) ⇒ Object



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

def match(str, index = 0)
  match_data = MatchData.new(self, str)
  loop do
    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



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

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