Forum: Ruby String#split with regex argument

249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-03-11 13:03
irb(main):004:0> " now's  the time".scan(/ /)
=> [" ", " ", " ", " "]
irb(main):005:0> " now's  the time".split(/ /)
=> ["", "now's", "", "the", "time"]

I expected ["", "now's", "", "the", "", "time"].

How
String#split(http://www.omniref.com/ruby/v2_1_1/classes/String##split)
with regex argument working here actually ?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2014-03-11 13:34
(Received via mailing list)
On Tue, Mar 11, 2014 at 1:03 PM, Arup Rakshit <lists@ruby-forum.com>
wrote:
> irb(main):004:0> " now's  the time".scan(/ /)
> => [" ", " ", " ", " "]
> irb(main):005:0> " now's  the time".split(/ /)
> => ["", "now's", "", "the", "time"]
>
> I expected ["", "now's", "", "the", "", "time"].
>
> How
> String#split(http://www.omniref.com/ruby/v2_1_1/classes/String##split)
> with regex argument working here actually ?

You have two consecutive spaces which each serve as delimiter since
you are only matching a single space. So you get an empty string in
between.

irb(main):001:0> "a  b".split(/ /)
=> ["a", "", "b"]

You can remedy by using a quatifier:

irb(main):002:0> "a  b".split(/ +/)
=> ["a", "b"]

It's probably easier to see with a different char

irb(main):003:0> "aXXb".split(/X/)
=> ["a", "", "b"]
irb(main):004:0> "aXXb".split(/X+/)
=> ["a", "b"]

Kind regards

robert
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-03-11 13:48
Robert Klemme wrote in post #1139487:
> On Tue, Mar 11, 2014 at 1:03 PM, Arup Rakshit <lists@ruby-forum.com>


> irb(main):003:0> "aXXb".split(/X/)
> => ["a", "", "b"]

Okay. Let me tell you then, what I understood from it.

When first `X` found string has been splitted as ["a","Xb"]. Now on the
second unprocessed substring "Xb", again 'X' matched, so it has benn
splitted as ["","b"]. And finally `["a", "", "b"]`. Am I correct ?
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2014-03-11 16:29
(Received via mailing list)
On Tue, Mar 11, 2014 at 1:48 PM, Arup Rakshit <lists@ruby-forum.com>
wrote:
> second unprocessed substring "Xb", again 'X' matched, so it has benn
> splitted as ["","b"]. And finally `["a", "", "b"]`. Am I correct ?

Roughly.  I can't say whether it is actually done that way but the
rule is simple: String#split will return everything between matches of
separator regexp.

You can imagine working like this

irb(main):001:0> def split s, re
irb(main):002:1> a = []
irb(main):003:1> pos = 0
irb(main):004:1> s.scan(re) {|m| n = $`.length; a << s[pos...n]; pos =
n + m.length}
irb(main):005:1> a << s[pos..-1]
irb(main):006:1> return a
irb(main):007:1> end
=> nil
irb(main):008:0> split "aXXb", /X/
=> ["a", "", "b"]

Cheers

robert
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.