Ruby 1.8.0 and =~

Hi, rubyists.

I have been out of it for a bit and have just started plating with 1.8.0.

I notice a new warning around =~
“warning: string =~ string will be obsolete; use explicit regexp”

Can someone please tell me the easy way to redo the following?

arr.each { |item|
if item =~ /blah/
info = item.split(/:/)
@store[key] = "#{val}:#{info[1].strip}:"
end
}

TIA,

···


-mark.


Mark Probert probertm@NOSPAM_nortelnetworks.com
Nortel Networks ph. (613) 768-1082

All opinions expressed are my own and do not
reflect in any way those of Nortel Networks.

Hi, rubyists.

I have been out of it for a bit and have just started plating with 1.8.0.

I notice a new warning around =~
“warning: string =~ string will be obsolete; use explicit regexp”

Can someone please tell me the easy way to redo the following?

arr.each { |item|
if item =~ /blah/
info = item.split(/:/)
@store[key] = “#{val}:#{info[1].strip}:”
end
}

No need to redo that one… the item on the
right of =~ is a regex, not a string.

Hal

···

----- Original Message -----
From: “Mark Probert” <probertm@NOSPAM_acm.org.web-hosting.com>
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, August 11, 2003 1:19 PM
Subject: ruby 1.8.0 and =~

This code works fine in 1.8.0.

What gives the waring is:

arr.each { |item|
   if item =~ "blah"  # see string on rhs of String#=~
     info = item.split(/:/)
     @store[key] = "#{val}:#{info[1].strip}:"
   end
}

Just make sure you use a Regexp on the right side of a =~.

···

On Aug 12, Mark Probert wrote:

arr.each { |item|
if item =~ /blah/
info = item.split(/:/)
@store[key] = “#{val}:#{info[1].strip}:”
end
}

Hi,

···

At Tue, 12 Aug 2003 03:19:29 +0900, Mark Probert wrote:

Can someone please tell me the easy way to redo the following?

Enumerable#grep might make it easier a bit.

arr.grep(/blah/) { |item|
info = item.split(/:/)
@store[key] = “#{val}:#{info[1].strip}:”
}


Nobu Nakada

“Hal E. Fulton” hal9000@hypermetrics.com did say …

Can someone please tell me the easy way to redo the following?

arr.each { |item|
if item =~ /blah/
info = item.split(/:/)
@store[key] = “#{val}:#{info[1].strip}:”
end
}

No need to redo that one… the item on the
right of =~ is a regex, not a string.

Doh!

-laugh-

I tried to use String.scan (it takes a regex) and happened to stumble
over the answer.

Thanks for the help!

···


-mark.


Mark Probert probertm@NOSPAM_nortelnetworks.com
Nortel Networks ph. (613) 768-1082

All opinions expressed are my own and do not
reflect in any way those of Nortel Networks.

Saluton!

  • Brett H. Williams; 2003-08-11, 20:28 UTC:

Just make sure you use a Regexp on the right side of a =~.

Note that you only have to make sure that you have one instance of
Regexp and one instance of String. No need to have the Regexp on the
right side. Here’s the warning:

ruby -e ‘p “a” =~ “ab”’
-e:1: warning: string =~ string will be obsolete; use explicit regexp
nil

And here is the reason for that warning and an illustration that you
can have String/Regexp on either side of ‘=~’

ruby -e ‘p /a/ =~ “ab”’
0

ruby -e ‘p “a” =~ /ab/’
nil

Gis,

Josef ‘Jupp’ Schugt

···


N’attribuez jamais à la malice ce que l’incompétence explique !
– Napoléon