This is a tough call, IMHO. It all depends on your mental description of
split. If you think of split as constructing an array of elements found in a
string, separated by a delimited, then returning makes sense because there
are no elements found. This progression makes a lot of sense ...
"a,b".split(',') => ['a', 'b'] # two elements found
"a".split(',') => ['a'] # one element found
"".split(',') => # zero elements found
However, if your mental model of split is that it starts with the original
string (well, a copy thereof) and breaks it apart whereever it finds a
delimiter, then this sequence makes sense...
"a,b".split(',') => ['a', 'b'] # Split between a and b
"a".split(',') => ['a'] # No delimiter found
"".split(',') => [''] # Again, no delimiter found
So when no delimiter is found, a list containing just the original string with
no splits makes sense in this model.
I will confess to finding myself in the first camp. It took some
experimentation before I saw the viewpoint of the second camp.
···
On Tuesday 07 December 2004 12:31 am, Yukihiro Matsumoto wrote:
Hi,
In message "Re: [rcr] String#split behaves odd" > > on Tue, 7 Dec 2004 14:16:06 +0900, "Peña, Botp" <botp@delmonte-phil.com> writes:
>imho, I think he meant
>
> != [""]
>
>I myself thought that string#split would return an array of strings w a
>minimum element of [""]
I don't get it. is an array of strings with zero elements.
--
-- Jim Weirich jim@weirichhouse.org http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct,
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)