String#split with nil first argument


(George Ogata) #1

When calling String#split with no arguments, it defaults to using $;,
which, by default, is set to nil. However, when calling String#split
with nil (including $; when it is set to nil) it raises exception.
This surprised me, which opposes the Principle of Least Surprise.

Oughtn’t String#split use $; when called with a nil first argument?


(David Alan Black) #2

Hello –

When calling String#split with no arguments, it defaults to using $;,

…only if $; is non-nil (because you can’t split on nil)

which, by default, is set to nil. However, when calling String#split
with nil (including $; when it is set to nil) it raises exception.
This surprised me, which opposes the Principle of Least Surprise.

Oughtn’t String#split use $; when called with a nil first argument?

Can’t you just not provide an argument? And if you want to use $;,
you can just assign to $; (and not provide an argument).

David

···

On Thu, 4 Jul 2002, George Ogata wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav


(George Ogata) #3

Hi,

David Alan Black wrote:

Hello –

When calling String#split with no arguments, it defaults to using $;,

…only if $; is non-nil (because you can’t split on nil)

which, by default, is set to nil. However, when calling String#split
with nil (including $; when it is set to nil) it raises exception.
This surprised me, which opposes the Principle of Least Surprise.

Oughtn’t String#split use $; when called with a nil first argument?

Can’t you just not provide an argument? And if you want to use $;,
you can just assign to $; (and not provide an argument).

David

The issue comes up when you use the second argument (the limit).

It’s hardly an inhibiting problem, but is seems an unnecessary quirk.
Other methods work by having an argument default to a value; here it’s not
the case. It acts like $;, except if $; is nil. Why not either make nil
mean ’ ', so it acts like $; all the time, or make nil mean “the behaviour
when the argument is not present”?

The former means we do string.split($;, 2) to mean “the behaviour with no
arguments but limit the array to 2 elements,” whereas the latter means we
do string.split(nil, 2). Currently we need string.split($; || ’ ', 2) (I
think), which seems silly.

···

On Thu, 4 Jul 2002, George Ogata wrote:


(Yukihiro Matsumoto) #4

Hi,

···

In message “Re: String#split with nil first argument” on 02/07/05, George Ogata g_ogata@optushome.com.au writes:

The former means we do string.split($;, 2) to mean “the behaviour with no
arguments but limit the array to 2 elements,” whereas the latter means we
do string.split(nil, 2). Currently we need string.split($; || ’ ', 2) (I
think), which seems silly.

Indeed. We will see split accepts nil as a first argument in the next
release.

						matz.