Skip to content

Commit 3b87e76

Browse files
BurdetteLamarpeterzhu2118
authored andcommitted
[DOC] Tweaks for String#rpartition
1 parent e930bd3 commit 3b87e76

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

doc/string/rpartition.rdoc

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,49 @@
11
Returns a 3-element array of substrings of +self+.
22

3-
Matches a pattern against +self+, scanning backwards from the end.
4-
The pattern is:
3+
Searches +self+ for a match of +pattern+, seeking the _last_ match.
54

6-
- +string_or_regexp+ itself, if it is a Regexp.
7-
- <tt>Regexp.quote(string_or_regexp)</tt>, if +string_or_regexp+ is a string.
5+
If +pattern+ is not matched, returns the array:
86

9-
If the pattern is matched, returns pre-match, last-match, post-match:
7+
["", "", self.dup]
108

11-
'hello'.rpartition('l') # => ["hel", "l", "o"]
12-
'hello'.rpartition('ll') # => ["he", "ll", "o"]
13-
'hello'.rpartition('h') # => ["", "h", "ello"]
14-
'hello'.rpartition('o') # => ["hell", "o", ""]
15-
'hello'.rpartition(/l+/) # => ["hel", "l", "o"]
16-
'hello'.rpartition('') # => ["hello", "", ""]
17-
'тест'.rpartition('т') # => ["тес", "т", ""]
18-
'こんにちは'.rpartition('に') # => ["こん", "に", "ちは"]
9+
If +pattern+ is matched, returns the array:
1910

20-
If the pattern is not matched, returns two empty strings and a copy of +self+:
11+
[pre_match, last_match, post_match]
2112

22-
'hello'.rpartition('x') # => ["", "", "hello"]
13+
where:
2314

24-
Related: String#partition, String#split.
15+
- +last_match+ is the last-found matching substring.
16+
- +pre_match+ and +post_match+ are the preceding and following substrings.
17+
18+
The pattern used is:
19+
20+
- +pattern+ itself, if it is a Regexp.
21+
- <tt>Regexp.quote(pattern)</tt>, if +pattern+ is a string.
22+
23+
Note that in the examples below, a returned string <tt>'hello'</tt> is a copy of +self+, not +self+.
24+
25+
If +pattern+ is a Regexp, searches for the last matching substring
26+
(also setting {pattern-matching global variables}[rdoc-ref:globals.md@Pattern+Matching]):
27+
28+
'hello'.rpartition(/l/) # => ["hel", "l", "o"]
29+
'hello'.rpartition(/ll/) # => ["he", "ll", "o"]
30+
'hello'.rpartition(/h/) # => ["", "h", "ello"]
31+
'hello'.rpartition(/o/) # => ["hell", "o", ""]
32+
'hello'.rpartition(//) # => ["hello", "", ""]
33+
'hello'.rpartition(/x/) # => ["", "", "hello"]
34+
'тест'.rpartition(/т/) # => ["тес", "т", ""]
35+
'こんにちは'.rpartition(/に/) # => ["こん", "に", "ちは"]
36+
37+
If +pattern+ is not a Regexp, converts it to a string (if it is not already one),
38+
then searches for the last matching substring
39+
(and does _not_ set {pattern-matching global variables}[rdoc-ref:globals.md@Pattern+Matching]):
40+
41+
'hello'.rpartition('l') # => ["hel", "l", "o"]
42+
'hello'.rpartition('ll') # => ["he", "ll", "o"]
43+
'hello'.rpartition('h') # => ["", "h", "ello"]
44+
'hello'.rpartition('o') # => ["hell", "o", ""]
45+
'hello'.rpartition('') # => ["hello", "", ""]
46+
'тест'.rpartition('т') # => ["тес", "т", ""]
47+
'こんにちは'.rpartition('に') # => ["こん", "に", "ちは"]
48+
49+
Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non--5CString].

string.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11196,7 +11196,7 @@ rb_str_partition(VALUE str, VALUE sep)
1119611196

1119711197
/*
1119811198
* call-seq:
11199-
* rpartition(sep) -> [head, match, tail]
11199+
* rpartition(pattern) -> [pre_match, last_match, post_match]
1120011200
*
1120111201
* :include: doc/string/rpartition.rdoc
1120211202
*

0 commit comments

Comments
 (0)