What is a result of "a b c\nd e f".split(" ")

First try to guess, then check.

Pretty interesting! Same result for \t and \n

···

On Thu, Sep 16, 2021, 10:55 PM hmdne <hmdne@airmail.cc> wrote:

First try to guess, then check.

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

In short - it does the same as split without an argument. It also removes whitespaces. It is documented. But it surprised me. This behavior can cause a lot of hard to debug problems.

···

On 9/17/21 8:04 AM, CSPablo Cortez wrote:

Pretty interesting! Same result for \t and \n

On Thu, Sep 16, 2021, 10:55 PM hmdne <hmdne@airmail.cc > <mailto:hmdne@airmail.cc>> wrote:

    First try to guess, then check.

    Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
    <mailto:ruby-talk-request@ruby-lang.org>?subject=unsubscribe>
    <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk
    <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

indeed. it's just odd that when explicitly specifying a space, the
behaviour defaults (imply) to a whitespace (ie " " same as /\s/) or as if
nothing was passed.

on my case, i now stay off from using space char when using split (when
needed, i use (/\ /) --sometimes i funny/weirdly like it since it looks
like an emoticon that seems to tease. lol).

some discussion on it and suggestions in
Feature #15771: Add `String#split` option to set `split_type string` with a single space separator - Ruby master - Ruby Issue Tracking System.

but i really prefer that this behaviour be remove or deprecated.

kind regards
--botp

···

On Fri, Sep 17, 2021 at 2:13 PM hmdne <hmdne@airmail.cc> wrote:

In short - it does the same as split without an argument. It also removes
whitespaces. It is documented. But it surprised me. This behavior can cause
a lot of hard to debug problems.

Quoting botp (botpena@gmail.com):

   but i really prefer that this behaviour be removed or
   deprecated.

If there has been work in optimizing (performance-wise) the
split-on-whitespace operation (compared to invoking the regex
mechanism), I would ask to leave the existing code, but have it
invoked by passing some sort of constant to the method - maybe symbol
:whitespace - instead of a single space.

Carlo

···

Subject: Re: What is a result of "a b c\nd e f".split(" ")
  Date: Fri 17 Sep 21 03:43:29PM +0800

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

I cannot remember when I last used String#split. I always find that
inferior to doing a proper regex match that identifies parts of the input
via groups.

Btw. I am pretty sure the behavior is the way it is to match awk's (from
the manpage):

In the special case that FS is a single space, fields are separated by

runs of spaces and/or tabs and/or newlines.

Cheers

robert

···

On Fri, Sep 17, 2021 at 9:53 AM Carlo E. Prelz <fluido@fluido.as> wrote:

        Subject: Re: What is a result of "a b c\nd e f".split(" ")
        Date: Fri 17 Sep 21 03:43:29PM +0800

Quoting botp (botpena@gmail.com):

> but i really prefer that this behaviour be removed or
> deprecated.

If there has been work in optimizing (performance-wise) the
split-on-whitespace operation (compared to invoking the regex
mechanism), I would ask to leave the existing code, but have it
invoked by passing some sort of constant to the method - maybe symbol
:whitespace - instead of a single space.

Carlo

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

--
[guy, jim, charlie, sho].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/