-----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 "]
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 "]
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
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: