From eb2b0c2a0d98b1a7929b47574cc2ec87c8c2e984 Mon Sep 17 00:00:00 2001 From: Kouhei Yanagita Date: Mon, 18 Nov 2024 12:47:41 +0900 Subject: [PATCH] [DOC] Fix the default `limit` of String#split We can't pass `nil` as the second parameter of `String#split`. Therefore, descriptions like "if limit is nil, ..." are not appropriate. --- doc/string/split.rdoc | 14 ++++++-------- string.c | 4 ++-- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/doc/string/split.rdoc b/doc/string/split.rdoc index 5ab065093b..95dda3c654 100644 --- a/doc/string/split.rdoc +++ b/doc/string/split.rdoc @@ -12,7 +12,7 @@ When +field_sep+ is $;: the split occurs just as if +field_sep+ were given as that string (see below). -When +field_sep+ is ' ' and +limit+ is +nil+, +When +field_sep+ is ' ' and +limit+ is +0+ (its default value), the split occurs at each sequence of whitespace: 'abc def ghi'.split(' ') => ["abc", "def", "ghi"] @@ -21,7 +21,7 @@ the split occurs at each sequence of whitespace: ''.split(' ') => [] When +field_sep+ is a string different from ' ' -and +limit+ is +nil+, +and +limit+ is +0+, the split occurs at each occurrence of +field_sep+; trailing empty substrings are not returned: @@ -33,7 +33,7 @@ trailing empty substrings are not returned: 'тест'.split('т') => ["", "ес"] 'こんにちは'.split('に') => ["こん", "ちは"] -When +field_sep+ is a Regexp and +limit+ is +nil+, +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: @@ -47,12 +47,10 @@ in the returned array: '1:2:3'.split(/(:)()()/, 2) # => ["1", ":", "", "", "2:3"] -As seen above, if +limit+ is +nil+, -trailing empty substrings are not returned; -the same is true if +limit+ is zero: +As seen above, if +limit+ is +0+, +trailing empty substrings are not returned: 'aaabcdaaa'.split('a') => ["", "", "", "bcd"] - 'aaabcdaaa'.split('a', 0) # => ["", "", "", "bcd"] If +limit+ is positive integer +n+, no more than n - 1- splits occur, so that at most +n+ substrings are returned, @@ -67,7 +65,7 @@ and trailing empty substrings are included: Note that if +field_sep+ is a \Regexp containing groups, their matches are in the returned array, but do not count toward the limit. -If +limit+ is negative, it behaves the same as if +limit+ was +nil+, +If +limit+ is negative, it behaves the same as if +limit+ was zero, meaning that there is no limit, and trailing empty substrings are included: diff --git a/string.c b/string.c index d80d214b50..99e236d12a 100644 --- a/string.c +++ b/string.c @@ -9172,8 +9172,8 @@ literal_split_pattern(VALUE spat, split_type_t default_type) /* * call-seq: - * split(field_sep = $;, limit = nil) -> array - * split(field_sep = $;, limit = nil) {|substring| ... } -> self + * split(field_sep = $;, limit = 0) -> array + * split(field_sep = $;, limit = 0) {|substring| ... } -> self * * :include: doc/string/split.rdoc *