Class: MatchData
Instance Method Summary collapse
- #last_match_index ⇒ Object
- #post_sub_match(index) ⇒ Object
- #pre_sub_match(index) ⇒ Object
- #replace(replace_string) ⇒ Object
- #sym2index(sym) ⇒ Object
Instance Method Details
#last_match_index ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/core_ext/match_data.rb', line 34 def last_match_index index = length - 1 to_a.reverse.each do |sub_match| return index if ! sub_match.nil? index -= 1 end end |
#post_sub_match(index) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/core_ext/match_data.rb', line 52 def post_sub_match(index) if index.abs >= length raise ArgumentError, "absolute value of index greater than length" end index = length + index if index < 0 string[offset(index)[1]...offset(0)[1]] end |
#pre_sub_match(index) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/core_ext/match_data.rb', line 42 def pre_sub_match(index) if index.abs >= length raise ArgumentError, "absolute value of index greater than length" end index = length + index if index < 0 string[offset(0)[0]...offset(index)[0]] end |
#replace(replace_string) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/core_ext/match_data.rb', line 19 def replace(replace_string) replace_string.gsub(/\\([0-9+&'`\\])/) {|sub| case $1 when '\\' '\\' when '`' pre_match when "'" post_match else to_a[sym2index($1)] end } end |
#sym2index(sym) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/core_ext/match_data.rb', line 4 def sym2index(sym) case sym when '+', :last_match, :last, "last_match", "last" last_match_index when '&', :all, "all" 0 when /[0-9]+/ sym.to_i when Integer sym else nil end end |