String Match method =~ error

The Ruby doc states the following for a String#=~

“cat o’ 9 tails” =~ ‘\d’ #=> nil

When I run what looks like the same command, I get the following error.

irb(main):001:0> “cat o’ 9 tails” =~ ‘\d’
TypeError: type mismatch: String given
from (irb):1:in `=~’
from (irb):1

I have tried this on a couple of different environments but always
Ruby 1.8.4. Can anyone provide me with any insight as to why I don’t
get the nil return value?

tim

Tim K. wrote:

The Ruby doc states the following for a String#=~

“cat o’ 9 tails” =~ ‘\d’ #=> nil

When I run what looks like the same command, I get the following error.

irb(main):001:0> “cat o’ 9 tails” =~ ‘\d’
TypeError: type mismatch: String given
from (irb):1:in `=~’
from (irb):1

I have tried this on a couple of different environments but always
Ruby 1.8.4. Can anyone provide me with any insight as to why I don’t
get the nil return value?

tim

“cat o’ 9 tails” =~ /\d/

On 5/26/06, mitchell [email protected] wrote:

    from (irb):1

I have tried this on a couple of different environments but always
Ruby 1.8.4. Can anyone provide me with any insight as to why I don’t
get the nil return value?

tim

“cat o’ 9 tails” =~ /\d/

ie. Ruby Doc is wrong?

Leslie V. wrote:

On 5/26/06, mitchell [email protected] wrote:

    from (irb):1

I have tried this on a couple of different environments but always
Ruby 1.8.4. Can anyone provide me with any insight as to why I don’t
get the nil return value?

tim

“cat o’ 9 tails” =~ /\d/

ie. Ruby Doc is wrong?

More likely just outdated. What “Ruby Doc” are you reading? I believe
this behavior changed between 1.6 and 1.8.

“T” == Tim K. [email protected] writes:

T> Does anyone happen to know what version this changed with?

1.8.2

svg% ./ruby -ve ‘“aa” =~ “a”’
ruby 1.8.2 (2004-12-25) [i686-linux]
-e:1:in `=~': type mismatch: String given (TypeError)
from -e:1
svg%

1.8.1 give

svg% ./ruby -ve ‘“aa” =~ “a”’
ruby 1.8.1 (2003-12-25) [i686-linux]
-e:1: warning: string =~ string will be obsolete; use explicit regexp
svg%

Guy Decoux

I found the answer. The Ruby doc is wrong for the Ruby 1.8.4.

http://www.ruby-doc.org/core/classes/String.html#M001849

According to the “Programming Ruby” book, at least one operand must be
a Regexp. Older versions of Ruby would convert the second operand to a
Regexp if both were Strings.

Does anyone happen to know what version this changed with?

Tim H. wrote:

tim

“cat o’ 9 tails” =~ /\d/

ie. Ruby Doc is wrong?

More likely just outdated.

Maybe, but it should be getting regenerate each night after fetching
the source from cvs

–
James B.

“I was born not knowing and have had only a little
time to change that here and there.”

  • Richard P. Feynman

Guy > 1.8.2

Thanks.

Tim >> ie. Ruby Doc is wrong?

Leslie > More likely just outdated. What “Ruby Doc” are you reading?

I thought the same but I did verify that the 1.8.4 docs are incorrect.
The link I posted was to the 1.8.4 core doc @ ruby-doc.org.