Skip to content

Commit 59a1a81

Browse files
[DOC] Enhanced RDoc for String#split (#5644)
* Enhanced RDoc for String#split * Enhanced RDoc for String#split * Enhanced RDoc for String#split * Enhanced RDoc for String#split * Enhanced RDoc for String#split
1 parent f38dcc7 commit 59a1a81

File tree

2 files changed

+93
-52
lines changed

2 files changed

+93
-52
lines changed

doc/string.rdoc

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,96 @@ class String
4747
def initialize(str = '', encoding: nil, capacity: nil)
4848
Primitive.rb_str_init(str, encoding, capacity)
4949
end
50+
51+
# call-seq:
52+
# split(separator = $;, limit = nil) -> array
53+
# split(separator = $;, limit = nil) {|substring| ... } -> self
54+
#
55+
# Returns an array of substrings of +self+
56+
# that are the result of splitting +self+
57+
# at each occurrence of the given +separator+.
58+
#
59+
# When argument +separator+ is <tt>$;</tt>:
60+
#
61+
# - If <tt>$;</tt> is +nil+ (its default value),
62+
# the split occurs just as if +separator+ were given as a space character
63+
# (see below).
64+
#
65+
# - If <tt>$;</tt> is a string,
66+
# the split ocurs just as if +separator+ were given as that string
67+
# (see below).
68+
#
69+
# When argument +separator+ is <tt>' '</tt> and argument +limit+ is +nil+,
70+
# the split occurs at each sequence of whitespace:
71+
#
72+
# 'abc def ghi'.split(' ') # => ["abc", "def", "ghi"]
73+
# "abc \n\tdef\t\n ghi".split(' ') # => ["abc", "def", "ghi"]
74+
# 'abc def ghi'.split(' ') # => ["abc", "def", "ghi"]
75+
# ''.split(' ') # => []
76+
#
77+
# When argument +separator+ is a string different from <tt>' '</tt>
78+
# and argument +limit+ is +nil+,
79+
# the split occurs at each occurrence of the separator;
80+
# trailing empty substrings are not returned:
81+
#
82+
# 'abracadabra'.split('ab') # => ["", "racad", "ra"]
83+
# 'aaabcdaaa'.split('a') # => ["", "", "", "bcd"]
84+
# ''.split('a') # => []
85+
# '3.14159'.split('1') # => ["3.", "4", "59"]
86+
# '!@#$%^$&*($)_+'.split('$') # => ["!@#", "%^", "&*(", ")_+"]
87+
# 'тест'.split('т') # => ["", "ес"]
88+
# 'こんにちは'.split('に') # => ["こん", "ちは"]
89+
#
90+
# When argument +separator+ is a Regexp and argument +limit+ is +nil+,
91+
# the split occurs at each occurrence of a match;
92+
# trailing empty substrings are not returned:
93+
#
94+
# 'abracadabra'.split(/ab/) # => ["", "racad", "ra"]
95+
# 'aaabcdaaa'.split(/a/) # => ["", "", "", "bcd"]
96+
# 'aaabcdaaa'.split(//) # => ["a", "a", "a", "b", "c", "d", "a", "a", "a"]
97+
# '1 + 1 == 2'.split(/\W+/) # => ["1", "1", "2"]
98+
#
99+
# If the \Regexp contains groups, their matches are also included
100+
# in the returned array:
101+
#
102+
# '1:2:3'.split(/(:)()()/, 2) # => ["1", ":", "", "", "2:3"]
103+
#
104+
# As seen above, if argument +limit+ is +nil+,
105+
# trailing empty substrings are not returned;
106+
# the same is true if +limit+ is zero:
107+
#
108+
# 'aaabcdaaa'.split('a') # => ["", "", "", "bcd"]
109+
# 'aaabcdaaa'.split('a', 0) # => ["", "", "", "bcd"]
110+
#
111+
# If +limit+ is positive integer +n+, no more than <tt>n - 1-</tt>
112+
# splits occur, so that at most +n+ substrings are returned,
113+
# and trailing empty substrings are included:
114+
#
115+
# 'aaabcdaaa'.split('a', 1) # => ["aaabcdaaa"]
116+
# 'aaabcdaaa'.split('a', 2) # => ["", "aabcdaaa"]
117+
# 'aaabcdaaa'.split('a', 5) # => ["", "", "", "bcd", "aa"]
118+
# 'aaabcdaaa'.split('a', 7) # => ["", "", "", "bcd", "", "", ""]
119+
# 'aaabcdaaa'.split('a', 8) # => ["", "", "", "bcd", "", "", ""]
120+
#
121+
# Note that if +separator+ is a \Regexp containing groups,
122+
# their matches are in the returned array, but do not count toward the limit.
123+
#
124+
# If +limit+ is negative, it behaves the same as if +limit+ was +nil+,
125+
# meaning that there is no limit,
126+
# and trailing empty substrings are included:
127+
#
128+
# 'aaabcdaaa'.split('a', -1) # => ["", "", "", "bcd", "", "", ""]
129+
#
130+
# If a block is given, it is called with each substring:
131+
#
132+
# 'abc def ghi'.split(' ') {|substring| p substring }
133+
#
134+
# Output:
135+
#
136+
# "abc"
137+
# "def"
138+
# "ghi"
139+
#
140+
def split; end
141+
50142
end

string.c

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8583,58 +8583,7 @@ literal_split_pattern(VALUE spat, split_type_t default_type)
85838583
}
85848584

85858585
/*
8586-
* call-seq:
8587-
* str.split(pattern=nil, [limit]) -> an_array
8588-
* str.split(pattern=nil, [limit]) {|sub| block } -> str
8589-
*
8590-
* Divides <i>str</i> into substrings based on a delimiter, returning an array
8591-
* of these substrings.
8592-
*
8593-
* If <i>pattern</i> is a String, then its contents are used as
8594-
* the delimiter when splitting <i>str</i>. If <i>pattern</i> is a single
8595-
* space, <i>str</i> is split on whitespace, with leading and trailing
8596-
* whitespace and runs of contiguous whitespace characters ignored.
8597-
*
8598-
* If <i>pattern</i> is a Regexp, <i>str</i> is divided where the
8599-
* pattern matches. Whenever the pattern matches a zero-length string,
8600-
* <i>str</i> is split into individual characters. If <i>pattern</i> contains
8601-
* groups, the respective matches will be returned in the array as well.
8602-
*
8603-
* If <i>pattern</i> is <code>nil</code>, the value of <code>$;</code> is used.
8604-
* If <code>$;</code> is <code>nil</code> (which is the default), <i>str</i> is
8605-
* split on whitespace as if ' ' were specified.
8606-
*
8607-
* If the <i>limit</i> parameter is omitted, trailing null fields are
8608-
* suppressed. If <i>limit</i> is a positive number, at most that number
8609-
* of split substrings will be returned (captured groups will be returned
8610-
* as well, but are not counted towards the limit).
8611-
* If <i>limit</i> is <code>1</code>, the entire
8612-
* string is returned as the only entry in an array. If negative, there is no
8613-
* limit to the number of fields returned, and trailing null fields are not
8614-
* suppressed.
8615-
*
8616-
* When the input +str+ is empty an empty Array is returned as the string is
8617-
* considered to have no fields to split.
8618-
*
8619-
* " now's the time ".split #=> ["now's", "the", "time"]
8620-
* " now's the time ".split(' ') #=> ["now's", "the", "time"]
8621-
* " now's the time".split(/ /) #=> ["", "now's", "", "the", "time"]
8622-
* "1, 2.34,56, 7".split(%r{,\s*}) #=> ["1", "2.34", "56", "7"]
8623-
* "hello".split(//) #=> ["h", "e", "l", "l", "o"]
8624-
* "hello".split(//, 3) #=> ["h", "e", "llo"]
8625-
* "hi mom".split(%r{\s*}) #=> ["h", "i", "m", "o", "m"]
8626-
*
8627-
* "mellow yellow".split("ello") #=> ["m", "w y", "w"]
8628-
* "1,2,,3,4,,".split(',') #=> ["1", "2", "", "3", "4"]
8629-
* "1,2,,3,4,,".split(',', 4) #=> ["1", "2", "", "3,4,,"]
8630-
* "1,2,,3,4,,".split(',', -4) #=> ["1", "2", "", "3", "4", "", ""]
8631-
*
8632-
* "1:2:3".split(/(:)()()/, 2) #=> ["1", ":", "", "", "2:3"]
8633-
*
8634-
* "".split(',', -1) #=> []
8635-
*
8636-
* If a block is given, invoke the block with each split substring.
8637-
*
8586+
* String#split is documented at doc/string.rdoc.
86388587
*/
86398588

86408589
static VALUE

0 commit comments

Comments
 (0)