String#split idea

Hi

I want wo suggest using 0 instead of a negative number for the 2nd arg, because result is the same for (-1..-n)
" 123 456 789 ".split " ",-1

whereas using a negative number should split beginning from the right side:
" 123 456 789 ".split " ", 2
=> ["123", "456 789 "]

" 123 456 789 ".split " ", -2
=> [" 123 456", "789"]

Would you find this helpful? (and possible to implement, because split is often used,
but I think not with negative numbers.

Opti

`mystring.reverse.split(" ", 2).reverse` ??

···

-----Original Message-----
From: ruby-talk [mailto:ruby-talk-bounces@ruby-lang.org] On Behalf Of Die
Optimisten
Sent: 20 January 2017 1:38 pm
To: Ruby-Talk
Subject: String#split idea

Hi

I want wo suggest using 0 instead of a negative number for the 2nd arg,
because result is the same for (-1..-n)
" 123 456 789 ".split " ",-1

whereas using a negative number should split beginning from the right side:
" 123 456 789 ".split " ", 2
=> ["123", "456 789 "]

" 123 456 789 ".split " ", -2
=> [" 123 456", "789"]

Would you find this helpful? (and possible to implement, because split
is often used,
but I think not with negative numbers.

Opti

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

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt;

I would assume it's very unlikely to get suggestions like this accepted
by the Ruby core team. Ruby is widely used in production and expected
to stay stable in its core functionality (maybe unlike some newer
languages that change heavily from release to release).
And as you mentioned yourself, String#split is often used (very often).
Incidentally, in the Ruby source code it's used several times with
negative limit argument.

Regards,
Marcus

···

Am 20.01.2017 um 14:37 schrieb Die Optimisten:

I want wo suggest using 0 instead of a negative number for the 2nd arg,
because result is the same for (-1..-n)
" 123 456 789 ".split " ",-1

whereas using a negative number should split beginning from the right side:
" 123 456 789 ".split " ", 2
=> ["123", "456 789 "]

" 123 456 789 ".split " ", -2
=> [" 123 456", "789"]

--
GitHub: stomar (Marcus Stollsteimer) · GitHub
PGP: 0x6B3A101A

`mystring.reverse.split(" ", 2).reverse` ??

Doh. Sorry. It's Friday.

`mystring.reverse.split(" ", 2).map(&:reverse).reverse`

And of course there is a cost for that on long strings.

What I was trying to say was, in Ruby, I think that we tend to solve any perceived problems with the core libraries ourselves. One could even, of course, do:

    class String
      unless defined(rightsplit)
        define_method(:rightsplit) { self.reverse.split(*args).map(&:reverse).reverse }
      end
    end

...or something...

Click here to view Company Information and Confidentiality Notice.<http://www.jameshall.co.uk/index.php/small-print/email-disclaimer&gt;

Hi
First thought of this, too -- but it's not THAT easy.
ok, a good idea
but split x, -n would be much nicer!

thanks
Opti

···

On 2017-01-20 16:37, Andy Jones wrote:

mystring.reverse.split(" ", 2).reverse` ??

Hi Andy,
I should first read all mails, then answer...

but split x, -n would be much nicer.
And more natural! -- Please vote for it if you'ld like it!

Just out of interest: Is there another solution avoiding the cost of reversing?
Another problem:
     str="abc def ghi"
     str[/(\s*(\w+))+/] # - How can I fill ALL $i (for each "\w+")??

thanks
Opti

···

On 2017-01-20 19:01, Die Optimisten wrote:
On 2017-01-20 16:37, Andy Jones wrote:

mystring.reverse.split(" ", 2).reverse` ??

Just out of interest: Is there another solution avoiding the cost of
reversing?

Yep. Iterate chars from the right. Or use regexen, if onigu is cheaper than
splitting (which can happen).

Another problem:
    str="abc def ghi"
    str[/(\s*(\w+))+/] # - How can I fill ALL $i (for each
"\w+")??

Check String#scan

thanks
Opti

···

On 21 Jan 2017 04:16, "Die Optimisten" <inform@die-optimisten.net> wrote: