# String Match method =~ error

**URL:** <https://rubytalk.org/t/string-match-method-error/27755>\
**Category:** ruby-talk\
**Created:** [26 May 2006 11:12 UTC](https://rubytalk.org/t/string-match-method-error/27755 "2006-05-26T11:12:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_Kuntz1](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@Tim\_Kuntz1](https://rubytalk.org/u/Tim_Kuntz1)\
**Post date:** [26 May 2006 11:12 UTC](https://rubytalk.org/t/string-match-method-error/27755/1 "2006-05-26T11:12:19Z")

</div>

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  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1:in `=~'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![mitchell1](https://avatars.discourse-cdn.com/v4/letter/m/9de053/32.png) [@mitchell1](https://rubytalk.org/u/mitchell1)\
**Post date:** [26 May 2006 11:17 UTC](https://rubytalk.org/t/string-match-method-error/27755/2 "2006-05-26T11:17:02Z")

</div>

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  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1:in `=~'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Leslie\_Viljoen1](https://avatars.discourse-cdn.com/v4/letter/l/f1d935/32.png) [@Leslie\_Viljoen1](https://rubytalk.org/u/Leslie_Viljoen1)\
**Post date:** [26 May 2006 11:41 UTC](https://rubytalk.org/t/string-match-method-error/27755/3 "2006-05-26T11:41:08Z")

</div>

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/

---

<div class="post-metadata">

**Author:** ![Tim\_Kuntz1](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@Tim\_Kuntz1](https://rubytalk.org/u/Tim_Kuntz1)\
**Post date:** [26 May 2006 12:04 UTC](https://rubytalk.org/t/string-match-method-error/27755/4 "2006-05-26T12:04:18Z")

</div>

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](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?

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter1](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@Tim\_Hunter1](https://rubytalk.org/u/Tim_Hunter1)\
**Post date:** [26 May 2006 12:05 UTC](https://rubytalk.org/t/string-match-method-error/27755/5 "2006-05-26T12:05:52Z")

</div>

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/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [26 May 2006 12:13 UTC](https://rubytalk.org/t/string-match-method-error/27755/6 "2006-05-26T12:13:13Z")

</div>

> 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)  
&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![James\_Britt4](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@James\_Britt4](https://rubytalk.org/u/James_Britt4)\
**Post date:** [27 May 2006 05:28 UTC](https://rubytalk.org/t/string-match-method-error/27755/7 "2006-05-27T05:28:40Z")

</div>

Tim Hunter wrote:

> Leslie Viljoen wrote:
> 
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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  
> &nbsp;&nbsp;time to change that here and there."  
> &nbsp;&nbsp;- Richard P. Feynman

---

<div class="post-metadata">

**Author:** ![Tim\_Kuntz1](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@Tim\_Kuntz1](https://rubytalk.org/u/Tim_Kuntz1)\
**Post date:** [27 May 2006 04:25 UTC](https://rubytalk.org/t/string-match-method-error/27755/8 "2006-05-27T04:25:52Z")

</div>

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](http://ruby-doc.org).
