Skip to content

Commit 9b22169

Browse files
[DOC] Tweaks for String#rindex
1 parent a0bf6d3 commit 9b22169

File tree

3 files changed

+55
-52
lines changed

3 files changed

+55
-52
lines changed

doc/string/index.rdoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ returns the index of the first matching substring in +self+:
1111
'тест'.index('с') # => 2 # Characters, not bytes.
1212
'こんにちは'.index('ち') # => 3
1313

14-
When +pattern is a Regexp, returns the index of the first match in +self+:
14+
When +pattern+ is a Regexp, returns the index of the first match in +self+:
1515

1616
'foo'.index(/o./) # => 1
1717
'foo'.index(/.o/) # => 0

doc/string/rindex.rdoc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
Returns the integer position of the _last_ substring that matches the given argument +pattern+,
2+
or +nil+ if none found.
3+
4+
When +pattern+ is a string, returns the index of the last matching substring in self:
5+
6+
'foo'.rindex('f') # => 0
7+
'foo'.rindex('o') # => 2
8+
'foo'.rindex('oo' # => 1
9+
'foo'.rindex('ooo') # => nil
10+
'тест'.rindex('т') # => 3
11+
'こんにちは'.rindex('ち') # => 3
12+
13+
When +pattern+ is a Regexp, returns the index of the last match in self:
14+
15+
'foo'.rindex(/f/) # => 0
16+
'foo'.rindex(/o/) # => 2
17+
'foo'.rindex(/oo/) # => 1
18+
'foo'.rindex(/ooo/) # => nil
19+
20+
When +offset+ is non-negative, it specifies the maximum starting position in the
21+
string to end the search:
22+
23+
'foo'.rindex('o', 0) # => nil
24+
'foo'.rindex('o', 1) # => 1
25+
'foo'.rindex('o', 2) # => 2
26+
'foo'.rindex('o', 3) # => 2
27+
28+
With negative integer argument +offset+,
29+
selects the search position by counting backward from the end of +self+:
30+
31+
'foo'.rindex('o', -1) # => 2
32+
'foo'.rindex('o', -2) # => 1
33+
'foo'.rindex('o', -3) # => nil
34+
'foo'.rindex('o', -4) # => nil
35+
36+
The last match means starting at the possible last position, not
37+
the last of longest matches:
38+
39+
'foo'.rindex(/o+/) # => 2
40+
$~ # => #<MatchData "o">
41+
42+
To get the last longest match, combine with negative lookbehind:
43+
44+
'foo'.rindex(/(?<!o)o+/) # => 1
45+
$~ # => #<MatchData "oo">
46+
47+
Or String#index with negative lookforward.
48+
49+
'foo'.index(/o+(?!.*o)/) # => 1
50+
$~ # => #<MatchData "oo">
51+
52+
Related: see {Querying}[rdoc-ref:String@Querying].

string.c

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4770,59 +4770,10 @@ rb_str_rindex(VALUE str, VALUE sub, long pos)
47704770

47714771
/*
47724772
* call-seq:
4773-
* rindex(substring, offset = self.length) -> integer or nil
4774-
* rindex(regexp, offset = self.length) -> integer or nil
4773+
* rindex(pattern, offset = self.length) -> integer or nil
47754774
*
4776-
* Returns the Integer index of the _last_ occurrence of the given +substring+,
4777-
* or +nil+ if none found:
4775+
* :include:doc/string/rindex.rdoc
47784776
*
4779-
* 'foo'.rindex('f') # => 0
4780-
* 'foo'.rindex('o') # => 2
4781-
* 'foo'.rindex('oo') # => 1
4782-
* 'foo'.rindex('ooo') # => nil
4783-
*
4784-
* Returns the Integer index of the _last_ match for the given Regexp +regexp+,
4785-
* or +nil+ if none found:
4786-
*
4787-
* 'foo'.rindex(/f/) # => 0
4788-
* 'foo'.rindex(/o/) # => 2
4789-
* 'foo'.rindex(/oo/) # => 1
4790-
* 'foo'.rindex(/ooo/) # => nil
4791-
*
4792-
* The _last_ match means starting at the possible last position, not
4793-
* the last of longest matches.
4794-
*
4795-
* 'foo'.rindex(/o+/) # => 2
4796-
* $~ #=> #<MatchData "o">
4797-
*
4798-
* To get the last longest match, needs to combine with negative
4799-
* lookbehind.
4800-
*
4801-
* 'foo'.rindex(/(?<!o)o+/) # => 1
4802-
* $~ #=> #<MatchData "oo">
4803-
*
4804-
* Or String#index with negative lookforward.
4805-
*
4806-
* 'foo'.index(/o+(?!.*o)/) # => 1
4807-
* $~ #=> #<MatchData "oo">
4808-
*
4809-
* Integer argument +offset+, if given and non-negative, specifies the maximum starting position in the
4810-
* string to _end_ the search:
4811-
*
4812-
* 'foo'.rindex('o', 0) # => nil
4813-
* 'foo'.rindex('o', 1) # => 1
4814-
* 'foo'.rindex('o', 2) # => 2
4815-
* 'foo'.rindex('o', 3) # => 2
4816-
*
4817-
* If +offset+ is a negative Integer, the maximum starting position in the
4818-
* string to _end_ the search is the sum of the string's length and +offset+:
4819-
*
4820-
* 'foo'.rindex('o', -1) # => 2
4821-
* 'foo'.rindex('o', -2) # => 1
4822-
* 'foo'.rindex('o', -3) # => nil
4823-
* 'foo'.rindex('o', -4) # => nil
4824-
*
4825-
* Related: String#index.
48264777
*/
48274778

48284779
static VALUE

0 commit comments

Comments
 (0)