Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/sixarm_ruby_string_index_after/string/index_after.rb

Overview

String#index_after.

Instance Method Summary collapse

Instance Method Details

#index_after(target, offset = 0) ⇒ Object

Return the index immediately after the first occurrence of the given substring orpattern (regexp) in str.

Return nil if not found.

The target parameter can be a string or a pattern (regexp).

The offset parameter specifies the position in the string to begin the search; the default is 0 i.e. the string start.

Examples:

"hello".index_after("ll") => 4

"hello".index_after(/[eo]/) => 2


24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sixarm_ruby_string_index_after/string/index_after.rb', line 24

def index_after(target, offset = 0)
  i = index(target, offset)
  return nil if nil == i
  length = case target
  when String
    target.length
  when Regexp
    m = match(target, offset)
    m ? m.to_s.length : nil
  else
    raise ArgumentError
  end
  return i + length
end