# Match with string instead of regexp fails

**URL:** <https://rubytalk.org/t/match-with-string-instead-of-regexp-fails/31798>\
**Category:** ruby-talk\
**Created:** [11 October 2006 17:00 UTC](https://rubytalk.org/t/match-with-string-instead-of-regexp-fails/31798 "2006-10-11T17:00:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt9](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@Matt9](https://rubytalk.org/u/Matt9)\
**Post date:** [11 October 2006 17:00 UTC](https://rubytalk.org/t/match-with-string-instead-of-regexp-fails/31798/1 "2006-10-11T17:00:15Z")

</div>

According to the 1.8.5 core API:

str =~ obj =\> fixnum or nil

Match. If obj is a Regexp, use it as a pattern to match against str. If  
obj is a String, look for it in str (similar to String#index). Returns  
the position the match starts, or nil if there is no match.

So I expect "cat" =~ "a" #=\> 1

But:

irb(main):013:0\> RUBY\_VERSION  
=\> "1.8.5"  
irb(main):014:0\> "cat" =~ "a"  
TypeError: type mismatch: String given  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):14:in `=~'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):14

An undocumented change? Sorry if this has come up before, I did try to  
find out. Thx - m.

> **···**
>
> from :0
> 
> --  
> matt neuburg, phd = [matt@tidbits.com](mailto:matt@tidbits.com), [http://www.tidbits.com/matt/](http://www.tidbits.com/matt/)  
> Tiger - [http://www.takecontrolbooks.com/tiger-customizing.html](http://www.takecontrolbooks.com/tiger-customizing.html)  
> AppleScript - [http://www.amazon.com/gp/product/0596102119](http://www.amazon.com/gp/product/0596102119)  
> Read TidBITS! It's free and smart. [http://www.tidbits.com](http://www.tidbits.com)

---

<div class="post-metadata">

**Author:** ![Jan\_Svitok](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Svitok](https://rubytalk.org/u/Jan_Svitok)\
**Post date:** [11 October 2006 19:06 UTC](https://rubytalk.org/t/match-with-string-instead-of-regexp-fails/31798/2 "2006-10-11T19:06:35Z")

</div>

In the CVS [1] there's a new documentation omitting the 'string'  
clause, so I suppose it's a documentation error.

If you know a bit of C you can see for yourself.

From the CVS history - the TypeError was added around 1.8.2, so it's  
there pretty long.

The doc was fixed between 1.8.5-pre1 and 1.8.5-pre2. Form that it  
seems that the docs on ruby-doc are rather old, and not exactly 1.8.5  
ones as they say...

[1] [http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/ruby/string.c?rev=1.182.2.53;content-type=text%2Fplain](http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/ruby/string.c?rev=1.182.2.53;content-type=text%2Fplain)

> **···**
>
> On 10/11/06, matt neuburg \<matt@tidbits.com\> wrote:
> 
> > According to the 1.8.5 core API:
> > 
> > str =~ obj =\> fixnum or nil
> > 
> > Match. If obj is a Regexp, use it as a pattern to match against str. If  
> > obj is a String, look for it in str (similar to String#index). Returns  
> > the position the match starts, or nil if there is no match.
> > 
> > So I expect "cat" =~ "a" #=\> 1
> > 
> > But:
> > 
> > irb(main):013:0\> RUBY\_VERSION  
> > =\> "1.8.5"  
> > irb(main):014:0\> "cat" =~ "a"  
> > TypeError: type mismatch: String given  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):14:in `=~'  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):14  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from :0
> > 
> > An undocumented change? Sorry if this has come up before, I did try to  
> > find out. Thx - m.

---

<div class="post-metadata">

**Author:** ![Matt9](https://avatars.discourse-cdn.com/v4/letter/m/ea5d25/32.png) [@Matt9](https://rubytalk.org/u/Matt9)\
**Post date:** [11 October 2006 20:40 UTC](https://rubytalk.org/t/match-with-string-instead-of-regexp-fails/31798/3 "2006-10-11T20:40:15Z")

</div>

Excellent, thanks. m.

> **···**
>
> Jan Svitok \<jan.svitok@gmail.com\> wrote:
> 
> > On 10/11/06, matt neuburg \<matt@tidbits.com\> wrote:  
> > \> According to the 1.8.5 core API:  
> > \>  
> > \> str =~ obj =\> fixnum or nil  
> > \>  
> > \> Match. If obj is a Regexp, use it as a pattern to match against str. If  
> > \> obj is a String, look for it in str (similar to String#index). Returns  
> > \> the position the match starts, or nil if there is no match.  
> > \>  
> > \> So I expect "cat" =~ "a" #=\> 1  
> > \>  
> > \> But:  
> > \>  
> > \> irb(main):013:0\> RUBY\_VERSION  
> > \> =\> "1.8.5"  
> > \> irb(main):014:0\> "cat" =~ "a"  
> > \> TypeError: type mismatch: String given  
> > \> from (irb):14:in `=~'  
> > \> from (irb):14  
> > \> from :0  
> > \>  
> > \> An undocumented change? Sorry if this has come up before, I did try to  
> > \> find out. Thx - m.
> > 
> > In the CVS [1] there's a new documentation omitting the 'string'  
> > clause, so I suppose it's a documentation error.
> > 
> > If you know a bit of C you can see for yourself.
> > 
> > From the CVS history - the TypeError was added around 1.8.2, so it's  
> > there pretty long.
> > 
> > The doc was fixed between 1.8.5-pre1 and 1.8.5-pre2. Form that it  
> > seems that the docs on ruby-doc are rather old, and not exactly 1.8.5  
> > ones as they say...
> > 
> > [1] [http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/ruby/string.c?rev=1.182.2.53](http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/ruby/string.c?rev=1.182.2.53);  
> > content-type=text%2Fplain
> 
> --  
> matt neuburg, phd = matt@tidbits.com, [Matt Neuburg’s Home Page](http://www.tidbits.com/matt/)  
> Tiger - [http://www.takecontrolbooks.com/tiger-customizing.html](http://www.takecontrolbooks.com/tiger-customizing.html)  
> AppleScript - [http://www.amazon.com/gp/product/0596102119](http://www.amazon.com/gp/product/0596102119)  
> Read TidBITS! It's free and smart. [http://www.tidbits.com](http://www.tidbits.com)
