summaryrefslogtreecommitdiff
path: root/doc/string/rpartition.rdoc
blob: 6a17b5e944bffc9b90d590dc68354fdcbd3a5031 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Returns a 3-element array of substrings of +self+.

Searches +self+ for a match of +pattern+, seeking the _last_ match.

If +pattern+ is not matched, returns the array:

  ["", "", self.dup]

If +pattern+ is matched, returns the array:

  [pre_match, last_match, post_match]

where:

- +last_match+ is the last-found matching substring.
- +pre_match+ and +post_match+ are the preceding and following substrings.

The pattern used is:

- +pattern+ itself, if it is a Regexp.
- <tt>Regexp.quote(pattern)</tt>, if +pattern+ is a string.

Note that in the examples below, a returned string <tt>'hello'</tt> is a copy of +self+, not +self+.

If +pattern+ is a Regexp, searches for the last matching substring
(also setting {matched-data global variables}[rdoc-ref:language/globals.md@Matched+Data]):

  'hello'.rpartition(/l/)     # => ["hel", "l", "o"]
  'hello'.rpartition(/ll/)    # => ["he", "ll", "o"]
  'hello'.rpartition(/h/)     # => ["", "h", "ello"]
  'hello'.rpartition(/o/)     # => ["hell", "o", ""]
  'hello'.rpartition(//)      # => ["hello", "", ""]
  'hello'.rpartition(/x/)     # => ["", "", "hello"]
  'тест'.rpartition(/т/)      # => ["тес", "т", ""]
  'こんにちは'.rpartition(/に/) # => ["こん", "に", "ちは"]

If +pattern+ is not a Regexp, converts it to a string (if it is not already one),
then searches for the last matching substring
(and does _not_ set {matched-data global variables}[rdoc-ref:language/globals.md@Matched+Data]):

  'hello'.rpartition('l')     # => ["hel", "l", "o"]
  'hello'.rpartition('ll')    # => ["he", "ll", "o"]
  'hello'.rpartition('h')     # => ["", "h", "ello"]
  'hello'.rpartition('o')     # => ["hell", "o", ""]
  'hello'.rpartition('')      # => ["hello", "", ""]
  'тест'.rpartition('т')      # => ["тес", "т", ""]
  'こんにちは'.rpartition('に') # => ["こん", "に", "ちは"]

Related: see {Converting to Non-String}[rdoc-ref:String@Converting+to+Non--5CString].