Tom Danielsen <tom / mnemonic.no> writes:

> While it works as documented in Pickaxe ( If pattern is omitted, the
> value of $; is used. If $; is nil (which is the default), str is split
> on whitespace as if ` ' were specified. ) I do find this behaviour
> somewhat surprising:
>
>     irb(main):004:0> "a b".split(" ")
>     => ["a", "b"]
>     irb(main):005:0> "a\tb".split(" ")
>     => ["a", "b"]
>     irb(main):006:0> "a b".split(/ /)
>     => ["a", "b"]
>     irb(main):007:0> "a\tb".split(/ /)
>     => ["a\tb"]
>     irb(main):008:0> 
>
> I think "a\tb".split(" ") => ["a", "b"] is quite counterintuitive...

This case follows the convention in Perl, where a split pattern of " "
(one explicit space, not in the form of a regexp) is a special case
which means to split on any occurrence of one or more whitespace
characters, ignoring any leading whitespace.

We also have this:

  irb(main):001:0> " a b".split(" ")
  => ["a", "b"]
  irb(main):002:0> " a b".split(/ /)
  => ["", "a", "b"]
  irb(main):003:0> "\ta\tb".split(" ")
  => ["a", "b"]
  irb(main):004:0> "\ta\tb".split(/\t/)
  => ["", "a", "b"]

It's a common occurrence to want to split lines that have fields
separated by arbitrary whitespace characters, and to ignore any leading
whitespace.  This usage of split() does that quite nicely.

This convention was almost certainly adopted deliberately, in order to
be consistent with some of the semantics of Perl's split() function.
Although it may seem counter-intuitive to people without prior Perl
experience, it's a very familiar construct for those who have been
working in Perl for a long time.

Just think of split(" ") as a special case which performs a very useful
function.


> % ruby -v
> ruby 1.8.1 (2003-12-25) [i386-freebsd5.1]
> % 
>
> regards,
> Tom

-- 
 Lloyd Zusman
 ljz / asfast.com
 God bless you.