# Re: match with string instead of regexp fails

**URL:** <https://www.ruby-forum.com/t/re-match-with-string-instead-of-regexp-fails/71921>\
**Category:** Ruby\
**Created:** [October 12, 2006, 12:54pm UTC](https://www.ruby-forum.com/t/re-match-with-string-instead-of-regexp-fails/71921 "2006-10-12T12:54:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![CameronS\_Gemma\_SUK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/45deac/32.png) [@CameronS\_Gemma\_SUK](https://www.ruby-forum.com/u/CameronS_Gemma_SUK)\
**Post date:** [October 12, 2006, 12:54pm UTC](https://www.ruby-forum.com/t/re-match-with-string-instead-of-regexp-fails/71921/1 "2006-10-12T12:54:12Z")

</div>

I thought the syntax was incorrect…

“cat” =~ “a” #=\> 1

Fails as it’s looking to match a reg ex not a string.

Should be:

“cat” =~ /a/ #=\> 1

Or am I wrong?!?!

Gem

* * *

This email and any attachments are confidential to the intended  
recipient and may also be privileged. If you are not the intended  
recipient please delete it from your system and notify the sender.  
You should not copy it or use it for any purpose nor disclose or  
distribute its contents to any other person.

* * *

---

<div class="post-metadata">

**Author:** ![CameronS\_Gemma\_SUK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/45deac/32.png) [@CameronS\_Gemma\_SUK](https://www.ruby-forum.com/u/CameronS_Gemma_SUK)\
**Post date:** [October 18, 2006, 10:39am UTC](https://www.ruby-forum.com/t/re-match-with-string-instead-of-regexp-fails/71921/2 "2006-10-18T10:39:30Z")

</div>

On 10/12/06, Cameron, Gemma (UK) [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Or am I wrong?!?!

Indeed, the example _is_ wrong (judged by current ruby). However the  
documentation states that:

str =~ obj =\> fixnum or nil

Match? 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. Otherwise, invokes obj.=~, passing str as an argument.  
The default =~ in Object returns false.

“cat o’ 9 tails” =~ ‘\d’ #=\> nil  
  
([class String - RDoc Documentation](http://ruby-doc.org/core/classes/String.html#M001453))

The question was why it doesn’t work. The answer: the online  
documentation is outdated (I don’t know how the docs in the install  
package look like, I haven’t installed 1.8.5 yet).
