Regexp warning in String.split?

irb(main):001:0> ‘a,b’.split(’,’)
=> [“a”, “b”]
irb(main):004:0> ‘a => b’.split(’ => ')
(irb):4: warning: string pattern instead of regexp; metacharacters
no longer eff
ective
=> [“a”, “b”]

Why do I get a warning on the second split call?

···

Chris
http://clabs.org/blogki

Because you should be using:

‘a => b’.split(/ => /)

(I don’t think ‘=’ or ‘>’ by themselves has significance in a regexp, but in
combination they might do - if you use odd things like assertions)

Cheers,

Brian.

···

On Fri, Aug 15, 2003 at 10:02:10PM +0900, Chris Morris wrote:

irb(main):001:0> 'a,b'.split(',')
=> ["a", "b"]
irb(main):004:0> 'a => b'.split(' => ')
(irb):4: warning: string pattern instead of regexp; metacharacters
no longer eff
ective
=> ["a", "b"]

Why do I get a warning on the second split call?

Hi –

···

On Fri, 15 Aug 2003, Chris Morris wrote:

irb(main):001:0> 'a,b'.split(',')
=> ["a", "b"]
irb(main):004:0> 'a => b'.split(' => ')
(irb):4: warning: string pattern instead of regexp; metacharacters
no longer eff
ective
=> ["a", "b"]

Why do I get a warning on the second split call?

In older Rubies, I believe that any string longer than one character,
as the argument to split, was interpreted as a regular expressions.
That changed at some point; strings are now always strings.

I thought the warning was supposed to disappear in the 1.8.0 final
release; either I’m wrong about that or it was just an oversight.

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Hi –

···

On Fri, 15 Aug 2003, Brian Candler wrote:

On Fri, Aug 15, 2003 at 10:02:10PM +0900, Chris Morris wrote:

irb(main):001:0> 'a,b'.split(',')
=> ["a", "b"]
irb(main):004:0> 'a => b'.split(' => ')
(irb):4: warning: string pattern instead of regexp; metacharacters
no longer eff
ective
=> ["a", "b"]

Why do I get a warning on the second split call?

Because you should be using:

‘a => b’.split(/ => /)

(I don’t think ‘=’ or ‘>’ by themselves has significance in a regexp, but in
combination they might do - if you use odd things like assertions)

It’s more that the behavior has changed; actually now it doesn’t
matter if they’re metacharacters, so it’s safer to use strings in that
position than it used to be (if a string will do in a given case, of
course).

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav