# String =~ string

**URL:** <https://rubytalk.org/t/string-string/28440>\
**Category:** ruby-talk\
**Created:** [24 June 2006 17:51 UTC](https://rubytalk.org/t/string-string/28440 "2006-06-24T17:51:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Young](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Alex\_Young](https://rubytalk.org/u/Alex_Young)\
**Post date:** [24 June 2006 17:51 UTC](https://rubytalk.org/t/string-string/28440/1 "2006-06-24T17:51:53Z")

</div>

[http://ruby-doc.org/core/classes/String.html#M001849](http://ruby-doc.org/core/classes/String.html#M001849) says this:

&nbsp;&nbsp;&nbsp;"cat o' 9 tails" =~ '\d' #=\> nil

I get this:

irb(main):001:0\> "cat o' 9 tails" =~ '\d'  
TypeError: type mismatch: String given  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1:in `=~'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):1

$ ruby -v  
ruby 1.8.4 (2005-12-24) [i386-mswin32]

Any clues?

> **···**
>
> --  
> Alex

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter4](https://avatars.discourse-cdn.com/v4/letter/t/ea5d25/32.png) [@Tim\_Hunter4](https://rubytalk.org/u/Tim_Hunter4)\
**Post date:** [24 June 2006 19:21 UTC](https://rubytalk.org/t/string-string/28440/2 "2006-06-24T19:21:41Z")

</div>

Alex Young wrote:

> [class String - RDoc Documentation](http://ruby-doc.org/core/classes/String.html#M001849) says this:
> 
> &nbsp;&nbsp;"cat o' 9 tails" =~ '\d' #=\> nil
> 
> I get this:
> 
> 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
> 
> $ ruby -v  
> ruby 1.8.4 (2005-12-24) [i386-mswin32]
> 
> Any clues?

Use a regexp:

irb(main):004:0\> "cat o' 9 tails" =~ /\d/  
=\> 7

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [24 June 2006 19:27 UTC](https://rubytalk.org/t/string-string/28440/3 "2006-06-24T19:27:25Z")

</div>

String#=~ used to automagically convert the second arg into a regexp. It doesn't do that anymore.

> **···**
>
> On Jun 24, 2006, at 1:51 PM, Alex Young wrote:
> 
> > [class String - RDoc Documentation](http://ruby-doc.org/core/classes/String.html#M001849) says this:
> > 
> > &nbsp;&nbsp;"cat o' 9 tails" =~ '\d' #=\> nil
> > 
> > I get this:
> > 
> > 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
> > 
> > $ ruby -v  
> > ruby 1.8.4 (2005-12-24) [i386-mswin32]
> > 
> > Any clues?
> > 
> > -- Alex

---

<div class="post-metadata">

**Author:** ![Alex\_Young](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Alex\_Young](https://rubytalk.org/u/Alex_Young)\
**Post date:** [24 June 2006 19:27 UTC](https://rubytalk.org/t/string-string/28440/4 "2006-06-24T19:27:17Z")

</div>

Timothy Hunter wrote:

> Alex Young wrote:
> 
> > [class String - RDoc Documentation](http://ruby-doc.org/core/classes/String.html#M001849) says this:
> > 
> > &nbsp;&nbsp;"cat o' 9 tails" =~ '\d' #=\> nil
> > 
> > I get this:
> > 
> > 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
> > 
> > $ ruby -v  
> > ruby 1.8.4 (2005-12-24) [i386-mswin32]
> > 
> > Any clues?
> 
> Use a regexp:
> 
> irb(main):004:0\> "cat o' 9 tails" =~ /\d/  
> =\> 7

That's not the point. Why is what I'm seeing different to what's in the docs? They explicitly say I should be able to use a string there, and I've just wasted half an hour tracking that bug down because of that.

> **···**
>
> --  
> Alex

---

<div class="post-metadata">

**Author:** ![Alex\_Young](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Alex\_Young](https://rubytalk.org/u/Alex_Young)\
**Post date:** [24 June 2006 19:30 UTC](https://rubytalk.org/t/string-string/28440/5 "2006-06-24T19:30:13Z")

</div>

Logan Capaldo wrote:

> String#=~ used to automagically convert the second arg into a regexp. It doesn't do that anymore.

Ah, ok. That makes sense. Why was it changed?

> **···**
>
> --  
> Alex

---

<div class="post-metadata">

**Author:** ![Matthew\_Harris](https://avatars.discourse-cdn.com/v4/letter/m/51bf81/32.png) [@Matthew\_Harris](https://rubytalk.org/u/Matthew_Harris)\
**Post date:** [26 June 2006 11:04 UTC](https://rubytalk.org/t/string-string/28440/6 "2006-06-26T11:04:56Z")

</div>

I'd really like similar behavior to be added back in. For example,  
converting to a Regexp, yes, but instead: 'foo' =~ 'o' would convert  
'o' such as: Regexp.new(Regexp.escape('o'))

> **···**
>
> On 6/25/06, Logan Capaldo \<logancapaldo@gmail.com\> wrote:
> 
> > On Jun 24, 2006, at 1:51 PM, Alex Young wrote:
> > 
> > \> [class String - RDoc Documentation](http://ruby-doc.org/core/classes/String.html#M001849) says this:  
> > \>  
> > \> "cat o' 9 tails" =~ '\d' #=\> nil  
> > \>  
> > \> I get this:  
> > \>  
> > \> irb(main):001:0\> "cat o' 9 tails" =~ '\d'  
> > \> TypeError: type mismatch: String given  
> > \> from (irb):1:in `=~'  
> > \> from (irb):1  
> > \>  
> > \> $ ruby -v  
> > \> ruby 1.8.4 (2005-12-24) [i386-mswin32]  
> > \>  
> > \> Any clues?  
> > \>  
> > \> --  
> > \> Alex  
> > \>
> > 
> > String#=~ used to automagically convert the second arg into a regexp.  
> > It doesn't do that anymore.
> 
> --  
> Matt
