Issue #5120 has been updated by Alexey Muranov.


I would like to summarize my feature request:

1. trailing empty fields should not be discarded
(it would make sense, however, to have a similar function which splits and discards initial and trailing empty fields, and returns at the first element of the array the number of initial empty fields discarded, and possibly as the last element of the array the number of trailing empty fields discarded)
2. a separate method for getting the array of letters (#letters?) should be implemented, split on the empty string should raise an error
(or otherwise it should always return the empty string as the first and the last elements: "a".split("")  # => ["", "a", ""] by analogy with  "a".split("a")  # => ["", ""] in the proposed implementation, but this is not very practical).

This is my opinion, please comment.

Alexey.
----------------------------------------
Feature #5120: String#split needs to be logical
http://redmine.ruby-lang.org/issues/5120

Author: Alexey Muranov
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 


I would call this a bug, but i am new to Ruby, so i report this as a feature request.

Here are examples showing a surprising and inconsistent behavior of String#split method:

"aa".split('a')  # => []
"aab".split('a')  # => ["", "", "b"]

"aaa".split('aa')  # => ["", "a"] 
"aaaa".split('aa')  # => []
"aaaaa".split('aa')  # => ["", "", "a"] 

"".split('')  # => []
"a".split('')  # => ["a"]

What is the definition of *split*?
In my opinion, there should be given a simple one that would make it clear what to expect.
For example:

  str1.split(str2) returns a maximal array of non-empty substrings of str1 which can be concatenated with copies of str2 to form str1.

Additional precisions can be made to this definition to make clear what to expect as the result of "baaab".split("aa").

Thanks for attention.


-- 
http://redmine.ruby-lang.org