Class: MatchData
Instance Method Summary collapse
- #breakdown ⇒ Object
- #display(first = 0, last = nil, open = '(', close = ')') ⇒ Object
- #display_breakdown ⇒ Object
- #replace_capture(c, s) ⇒ Object
-
#sub(repl, capture_num = 1) ⇒ Object
Replace the matching part of the string with match interpolation.
Instance Method Details
#breakdown ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/innate/regexp.rb', line 45 def breakdown s = [] length.times do |o| if offset(o) == [nil, nil] s << '' else s << (' ' * offset(o).first) + self[o] + (' ' * (string.length - offset(o).last)) end end s end |
#display(first = 0, last = nil, open = '(', close = ')') ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/innate/regexp.rb', line 23 def display(first = 0, last = nil, open = '(', close = ')') s = pre_match p = s.length e = [] (first...(last || length)).each do |o| next if offset(o) == [nil, nil] while e.last and e.last <= offset(o).first s << string[p...e.last] + close p = e.pop end s << string[p...offset(o).first] + open p = offset(o).first e << offset(o).last end while e.last s << string[p...e.last] + close p = e.pop end s << string[p..-1] return s end |
#display_breakdown ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/innate/regexp.rb', line 57 def display_breakdown r = "S: |#{string}|" breakdown.each_with_index do |s, i| r += "\n#{i}: |#{s}|" end r end |
#replace_capture(c, s) ⇒ Object
19 20 21 |
# File 'lib/innate/regexp.rb', line 19 def replace_capture(c, s) string[0, offset(c)[0]] + (s || '') + string[offset(c)[1]..-1] end |
#sub(repl, capture_num = 1) ⇒ Object
Replace the matching part of the string with match interpolation.
If there are any captures, replaces the first one, otherwise replaces the whole match.
Interpolates %n to the corresponding capture.
71 72 73 74 75 76 77 78 |
# File 'lib/innate/regexp.rb', line 71 def sub(repl, capture_num = 1) cn = capture_num || 1 t = repl.clone to_a[1..-1].each_with_index do |m, i| t.gsub!("%#{i + 1}", m ? m : '') end replace_capture self[cn] ? cn : 0, t end |