class Arugula::MatchData

Attributes

end_index[RW]
regexp[R]
start_index[RW]
string[R]

Public Class Methods

new(regexp, string) click to toggle source
# File lib/arugula/match_data.rb, line 4
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

Public Instance Methods

==(other) click to toggle source
# File lib/arugula/match_data.rb, line 68
def ==(other)
  return false unless other.is_a?(MatchData) || other.is_a?(::MatchData)
  string == other.string &&
    regexp == other.regexp &&
    captures == other.captures
end
add_capture(name, start_index, end_index) click to toggle source
# File lib/arugula/match_data.rb, line 12
def add_capture(name, start_index, end_index)
  @captures[name] = start_index...end_index
end
captures() click to toggle source
# File lib/arugula/match_data.rb, line 45
def captures
  @captures.map { |_name, range| range && @string[range] }
end
freeze() click to toggle source
Calls superclass method
# File lib/arugula/match_data.rb, line 59
def freeze
  @captures.freeze
  super
end
hash() click to toggle source
# File lib/arugula/match_data.rb, line 64
def hash
  @string.hash ^ @regexp.hash ^ @captures.hash
end
inspect() click to toggle source
# File lib/arugula/match_data.rb, line 29
def inspect
  captures_part = @captures.map do |name, range|
    " #{name}:#{dump_str(range && @string[range])}"
  end.join
  "#<MatchData #{dump_str(to_s)}#{captures_part}>"
end
length()
Alias for: size
post_match() click to toggle source
# File lib/arugula/match_data.rb, line 54
def post_match
  return '' if end_index == string.size
  @string[end_index..-1]
end
pre_match() click to toggle source
# File lib/arugula/match_data.rb, line 49
def pre_match
  return '' if start_index == 0
  @string[0...start_index]
end
reset_captures!() click to toggle source
# File lib/arugula/match_data.rb, line 16
def reset_captures!
  @captures.keys.each do |key|
    @captures[key] = nil
  end
end
size() click to toggle source
# File lib/arugula/match_data.rb, line 40
def size
  @captures.size + 1
end
Also aliased as: length
to_a() click to toggle source
# File lib/arugula/match_data.rb, line 36
def to_a
  captures.unshift(to_s)
end
to_s() click to toggle source
# File lib/arugula/match_data.rb, line 25
def to_s
  @string[start_index...end_index]
end

Private Instance Methods

dump_str(str) click to toggle source
# File lib/arugula/match_data.rb, line 77
def dump_str(str)
  str.nil? ? 'nil' : str.dump
end