Class: Arugula::MatchData
- Inherits:
-
Object
- Object
- Arugula::MatchData
- Defined in:
- lib/arugula/match_data.rb
Instance Attribute Summary collapse
-
#end_index ⇒ Object
Returns the value of attribute end_index.
-
#regexp ⇒ Object
readonly
Returns the value of attribute regexp.
-
#start_index ⇒ Object
Returns the value of attribute start_index.
-
#string ⇒ Object
readonly
Returns the value of attribute string.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #add_capture(name, start_index, end_index) ⇒ Object
- #captures ⇒ Object
- #freeze ⇒ Object
- #hash ⇒ Object
-
#initialize(regexp, string) ⇒ MatchData
constructor
A new instance of MatchData.
- #inspect ⇒ Object
- #post_match ⇒ Object
- #pre_match ⇒ Object
- #reset_captures! ⇒ Object
- #size ⇒ Object (also: #length)
- #to_a ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(regexp, string) ⇒ MatchData
Returns a new instance of MatchData.
4 5 6 7 8 9 10 |
# 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 |
Instance Attribute Details
#end_index ⇒ Object
Returns the value of attribute end_index.
23 24 25 |
# File 'lib/arugula/match_data.rb', line 23 def end_index @end_index end |
#regexp ⇒ Object (readonly)
Returns the value of attribute regexp.
3 4 5 |
# File 'lib/arugula/match_data.rb', line 3 def regexp @regexp end |
#start_index ⇒ Object
Returns the value of attribute start_index.
22 23 24 |
# File 'lib/arugula/match_data.rb', line 22 def start_index @start_index end |
#string ⇒ Object (readonly)
Returns the value of attribute string.
3 4 5 |
# File 'lib/arugula/match_data.rb', line 3 def string @string end |
Instance Method Details
#==(other) ⇒ Object
68 69 70 71 72 73 |
# 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) ⇒ Object
12 13 14 |
# File 'lib/arugula/match_data.rb', line 12 def add_capture(name, start_index, end_index) @captures[name] = start_index...end_index end |
#captures ⇒ Object
45 46 47 |
# File 'lib/arugula/match_data.rb', line 45 def captures @captures.map { |_name, range| range && @string[range] } end |
#freeze ⇒ Object
59 60 61 62 |
# File 'lib/arugula/match_data.rb', line 59 def freeze @captures.freeze super end |
#hash ⇒ Object
64 65 66 |
# File 'lib/arugula/match_data.rb', line 64 def hash @string.hash ^ @regexp.hash ^ @captures.hash end |
#inspect ⇒ Object
29 30 31 32 33 34 |
# 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 |
#post_match ⇒ Object
54 55 56 57 |
# File 'lib/arugula/match_data.rb', line 54 def post_match return '' if end_index == string.size @string[end_index..-1] end |
#pre_match ⇒ Object
49 50 51 52 |
# File 'lib/arugula/match_data.rb', line 49 def pre_match return '' if start_index == 0 @string[0...start_index] end |
#reset_captures! ⇒ Object
16 17 18 19 20 |
# File 'lib/arugula/match_data.rb', line 16 def reset_captures! @captures.keys.each do |key| @captures[key] = nil end end |
#size ⇒ Object Also known as: length
40 41 42 |
# File 'lib/arugula/match_data.rb', line 40 def size @captures.size + 1 end |
#to_a ⇒ Object
36 37 38 |
# File 'lib/arugula/match_data.rb', line 36 def to_a captures.unshift(to_s) end |
#to_s ⇒ Object
25 26 27 |
# File 'lib/arugula/match_data.rb', line 25 def to_s @string[start_index...end_index] end |