@@ -15,31 +15,31 @@ When +field_sep+ is <tt>$;</tt>:
1515When +field_sep+ is <tt>' '</tt> and +limit+ is +0+ (its default value),
1616the split occurs at each sequence of whitespace:
1717
18- 'abc def ghi'.split(' ') => ["abc", "def", "ghi"]
18+ 'abc def ghi'.split(' ') # => ["abc", "def", "ghi"]
1919 "abc \n\tdef\t\n ghi".split(' ') # => ["abc", "def", "ghi"]
20- 'abc def ghi'.split(' ') => ["abc", "def", "ghi"]
21- ''.split(' ') => []
20+ 'abc def ghi'.split(' ') # => ["abc", "def", "ghi"]
21+ ''.split(' ') # => []
2222
2323When +field_sep+ is a string different from <tt>' '</tt>
2424and +limit+ is +0+,
2525the split occurs at each occurrence of +field_sep+;
2626trailing empty substrings are not returned:
2727
28- 'abracadabra'.split('ab') => ["", "racad", "ra"]
29- 'aaabcdaaa'.split('a') => ["", "", "", "bcd"]
30- ''.split('a') => []
31- '3.14159'.split('1') => ["3.", "4", "59"]
28+ 'abracadabra'.split('ab') # => ["", "racad", "ra"]
29+ 'aaabcdaaa'.split('a') # => ["", "", "", "bcd"]
30+ ''.split('a') # => []
31+ '3.14159'.split('1') # => ["3.", "4", "59"]
3232 '!@#$%^$&*($)_+'.split('$') # => ["!@#", "%^", "&*(", ")_+"]
33- 'тест'.split('т') => ["", "ес"]
34- 'こんにちは'.split('に') => ["こん", "ちは"]
33+ 'тест'.split('т') # => ["", "ес"]
34+ 'こんにちは'.split('に') # => ["こん", "ちは"]
3535
3636When +field_sep+ is a Regexp and +limit+ is +0+,
3737the split occurs at each occurrence of a match;
3838trailing empty substrings are not returned:
3939
4040 'abracadabra'.split(/ab/) # => ["", "racad", "ra"]
41- 'aaabcdaaa'.split(/a/) => ["", "", "", "bcd"]
42- 'aaabcdaaa'.split(//) => ["a", "a", "a", "b", "c", "d", "a", "a", "a"]
41+ 'aaabcdaaa'.split(/a/) # => ["", "", "", "bcd"]
42+ 'aaabcdaaa'.split(//) # => ["a", "a", "a", "b", "c", "d", "a", "a", "a"]
4343 '1 + 1 == 2'.split(/\W+/) # => ["1", "1", "2"]
4444
4545If the \Regexp contains groups, their matches are also included
@@ -50,7 +50,7 @@ in the returned array:
5050As seen above, if +limit+ is +0+,
5151trailing empty substrings are not returned:
5252
53- 'aaabcdaaa'.split('a') => ["", "", "", "bcd"]
53+ 'aaabcdaaa'.split('a') # => ["", "", "", "bcd"]
5454
5555If +limit+ is positive integer +n+, no more than <tt>n - 1-</tt>
5656splits occur, so that at most +n+ substrings are returned,
@@ -71,7 +71,7 @@ and trailing empty substrings are included:
7171
7272 'aaabcdaaa'.split('a', -1) # => ["", "", "", "bcd", "", "", ""]
7373
74- If a block is given, it is called with each substring:
74+ If a block is given, it is called with each substring and returns +self+ :
7575
7676 'abc def ghi'.split(' ') {|substring| p substring }
7777
@@ -80,5 +80,20 @@ Output:
8080 "abc"
8181 "def"
8282 "ghi"
83+ => "abc def ghi"
84+
85+ Note that the above example is functionally the same as calling +#each+ after
86+ +#split+ and giving the same block. However, the above example has better
87+ performance because it avoids the creation of an intermediate array. Also,
88+ note the different return values.
89+
90+ 'abc def ghi'.split(' ').each {|substring| p substring }
91+
92+ Output:
93+
94+ "abc"
95+ "def"
96+ "ghi"
97+ => ["abc", "def", "ghi"]
8398
8499Related: String#partition, String#rpartition.
0 commit comments