Class: Arugula::MatchData

Inherits:
Object
  • Object
show all
Defined in:
lib/arugula/match_data.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(regexp, string) ⇒ MatchData

Returns a new instance of MatchData.



3
4
5
6
7
8
9
# File 'lib/arugula/match_data.rb', line 3

def initialize(regexp, string)
  # require "awesome_print"
  # ap regexp, raw: true
  @regexp = regexp
  @string = string.dup.freeze
  @captures = Hash[regexp.captures.map { |c| [c.name, nil] }]
end

Instance Attribute Details

#end_indexObject

Returns the value of attribute end_index.



16
17
18
# File 'lib/arugula/match_data.rb', line 16

def end_index
  @end_index
end

#start_indexObject

Returns the value of attribute start_index.



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

def start_index
  @start_index
end

Instance Method Details

#add_capture(name, start_index, end_index) ⇒ Object



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

def add_capture(name, start_index, end_index)
  @captures[name] = start_index...end_index
end

#freezeObject



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

def freeze
  @captures.freeze
  super
end

#inspectObject



22
23
24
25
26
27
# File 'lib/arugula/match_data.rb', line 22

def inspect
  captures_part = @captures.map do |name, range|
    " #{name}:#{@string[range].dump}"
  end.join
  "#<MatchData #{to_s.dump}#{captures_part}>"
end

#to_aObject



29
30
31
# File 'lib/arugula/match_data.rb', line 29

def to_a
  @captures.map { |_name, range| @string[range] }.unshift(to_s)
end

#to_sObject



18
19
20
# File 'lib/arugula/match_data.rb', line 18

def to_s
  @string[start_index...end_index]
end