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 Kuntz 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/

···

--
Posted via http://www.ruby-forum.com/\.

ie. Ruby Doc is wrong?

···

On 5/26/06, mitchell <fs8206@yahoo.com> wrote:

Tim Kuntz 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/

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?

Leslie Viljoen wrote:

···

On 5/26/06, mitchell <fs8206@yahoo.com> 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.

--
Posted via http://www.ruby-forum.com/\.

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

Tim Hunter wrote:

Leslie Viljoen 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.

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

···

On 5/26/06, mitchell <fs8206@yahoo.com> wrote:

--
James Britt

"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.