summaryrefslogtreecommitdiff
path: root/doc/string
diff options
context:
space:
mode:
authorDamian C. Rossney <[email protected]>2025-04-23 00:05:30 -0400
committerGitHub <[email protected]>2025-04-23 13:05:30 +0900
commit6029781984b9db6e1a7f4542e6a6f094876098f3 (patch)
tree60f1c2ef44ac1b9c2a9a1fd6425cd5084cc2d029 /doc/string
parentc1dbd01c6796adb64edc6136134be8271b933bb8 (diff)
[DOC] Update for String#split
Highlight the performance advantages of calling `string.split` with a block, instead of `string.split.each` with the same block. Includes other minor formatting corrections.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/13153 Merged-By: nobu <[email protected]>
Diffstat (limited to 'doc/string')
-rw-r--r--doc/string/split.rdoc41
1 files changed, 28 insertions, 13 deletions
diff --git a/doc/string/split.rdoc b/doc/string/split.rdoc
index 95dda3c654..131c14b83f 100644
--- a/doc/string/split.rdoc
+++ b/doc/string/split.rdoc
@@ -15,31 +15,31 @@ When +field_sep+ is <tt>$;</tt>:
When +field_sep+ is <tt>' '</tt> and +limit+ is +0+ (its default value),
the split occurs at each sequence of whitespace:
- 'abc def ghi'.split(' ') => ["abc", "def", "ghi"]
+ 'abc def ghi'.split(' ') # => ["abc", "def", "ghi"]
"abc \n\tdef\t\n ghi".split(' ') # => ["abc", "def", "ghi"]
- 'abc def ghi'.split(' ') => ["abc", "def", "ghi"]
- ''.split(' ') => []
+ 'abc def ghi'.split(' ') # => ["abc", "def", "ghi"]
+ ''.split(' ') # => []
When +field_sep+ is a string different from <tt>' '</tt>
and +limit+ is +0+,
the split occurs at each occurrence of +field_sep+;
trailing empty substrings are not returned:
- 'abracadabra'.split('ab') => ["", "racad", "ra"]
- 'aaabcdaaa'.split('a') => ["", "", "", "bcd"]
- ''.split('a') => []
- '3.14159'.split('1') => ["3.", "4", "59"]
+ 'abracadabra'.split('ab') # => ["", "racad", "ra"]
+ 'aaabcdaaa'.split('a') # => ["", "", "", "bcd"]
+ ''.split('a') # => []
+ '3.14159'.split('1') # => ["3.", "4", "59"]
'!@#$%^$&*($)_+'.split('$') # => ["!@#", "%^", "&*(", ")_+"]
- 'тест'.split('т') => ["", "ес"]
- 'こんにちは'.split('に') => ["こん", "ちは"]
+ 'тест'.split('т') # => ["", "ес"]
+ 'こんにちは'.split('に') # => ["こん", "ちは"]
When +field_sep+ is a Regexp and +limit+ is +0+,
the split occurs at each occurrence of a match;
trailing empty substrings are not returned:
'abracadabra'.split(/ab/) # => ["", "racad", "ra"]
- 'aaabcdaaa'.split(/a/) => ["", "", "", "bcd"]
- 'aaabcdaaa'.split(//) => ["a", "a", "a", "b", "c", "d", "a", "a", "a"]
+ 'aaabcdaaa'.split(/a/) # => ["", "", "", "bcd"]
+ 'aaabcdaaa'.split(//) # => ["a", "a", "a", "b", "c", "d", "a", "a", "a"]
'1 + 1 == 2'.split(/\W+/) # => ["1", "1", "2"]
If the \Regexp contains groups, their matches are also included
@@ -50,7 +50,7 @@ in the returned array:
As seen above, if +limit+ is +0+,
trailing empty substrings are not returned:
- 'aaabcdaaa'.split('a') => ["", "", "", "bcd"]
+ 'aaabcdaaa'.split('a') # => ["", "", "", "bcd"]
If +limit+ is positive integer +n+, no more than <tt>n - 1-</tt>
splits occur, so that at most +n+ substrings are returned,
@@ -71,7 +71,7 @@ and trailing empty substrings are included:
'aaabcdaaa'.split('a', -1) # => ["", "", "", "bcd", "", "", ""]
-If a block is given, it is called with each substring:
+If a block is given, it is called with each substring and returns +self+:
'abc def ghi'.split(' ') {|substring| p substring }
@@ -80,5 +80,20 @@ Output:
"abc"
"def"
"ghi"
+ => "abc def ghi"
+
+Note that the above example is functionally the same as calling +#each+ after
++#split+ and giving the same block. However, the above example has better
+performance because it avoids the creation of an intermediate array. Also,
+note the different return values.
+
+ 'abc def ghi'.split(' ').each {|substring| p substring }
+
+Output:
+
+ "abc"
+ "def"
+ "ghi"
+ => ["abc", "def", "ghi"]
Related: String#partition, String#rpartition.