|
1 | 1 | Returns a 3-element array of substrings of +self+. |
2 | 2 |
|
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. |
5 | 4 |
|
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: |
8 | 6 |
|
9 | | -If the pattern is matched, returns pre-match, last-match, post-match: |
| 7 | + ["", "", self.dup] |
10 | 8 |
|
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: |
19 | 10 |
|
20 | | -If the pattern is not matched, returns two empty strings and a copy of +self+: |
| 11 | + [pre_match, last_match, post_match] |
21 | 12 |
|
22 | | - 'hello'.rpartition('x') # => ["", "", "hello"] |
| 13 | +where: |
23 | 14 |
|
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]. |
0 commit comments