# Regexp ops: get rid of !=~?

**URL:** <https://www.ruby-forum.com/t/regexp-ops-get-rid-of/159987>\
**Category:** Ruby\
**Created:** [February 10, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/regexp-ops-get-rid-of/159987 "2009-02-10T18:31:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gyngve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e95f7d/32.png) [@gyngve](https://www.ruby-forum.com/u/gyngve)\
**Post date:** [February 10, 2009, 6:31pm UTC](https://www.ruby-forum.com/t/regexp-ops-get-rid-of/159987/1 "2009-02-10T18:31:07Z")

</div>

The proper way to see if something doesn’t match is  
“foo” !~ /bar/

The ~/blah/ operator exists to apply to $\_

Unfortunately this means that

“foo” !=~ /bar/

does

“foo”.!= (~bar), which doesn’t produce an error, even though it’s  
basically  
garbage.  
Instead it fails silently by returning true (nil or an number are not  
strings).

Is there a way w/ Ruby to define a mega !=~ operator and either have it  
do  
!~ or have it throw an exception?  
Or an easy way w/ Ruby to have the parser warn about it?

Alas google is not good for searching for !=~, so apologies if this has  
been  
previously discussed.

Thanks,  
Gary

---

<div class="post-metadata">

**Author:** ![gyngve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e95f7d/32.png) [@gyngve](https://www.ruby-forum.com/u/gyngve)\
**Post date:** [February 11, 2009, 3:59pm UTC](https://www.ruby-forum.com/t/regexp-ops-get-rid-of/159987/2 "2009-02-11T15:59:24Z")

</div>

Gary Y. wrote:

> “foo” !=~ /bar/
> 
> does
> 
> “foo”.!= (~bar)

Actually: “foo” != ~/bar/

> which doesn’t produce an error, even though it’s  
> basically  
> garbage.  
> Instead it fails silently by returning true (nil or an number are not  
> strings).

The problem here is ~/bar/

This is an ugly Perlism in Ruby which hardly anybody ever uses anyway.  
Search for Regexp#~ in ri.

Personally, I would be very happy to lose this entirely, together with  
all the other implicit tests and side-effects on $\_ (such as is done by  
Kernel#gets and IO#gets). Or at least have a flag to turn on this  
behaviour.

Regards,

Brian.

---

<div class="post-metadata">

**Author:** ![gyngve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/e95f7d/32.png) [@gyngve](https://www.ruby-forum.com/u/gyngve)\
**Post date:** [February 11, 2009, 4:02pm UTC](https://www.ruby-forum.com/t/regexp-ops-get-rid-of/159987/3 "2009-02-11T16:02:17Z")

</div>

Gary Y. wrote:

> Or an easy way w/ Ruby to have the parser warn about it?

irb(main):001:0\> Regexp.class\_eval{undef\_method :~}  
=\> Regexp  
irb(main):002:0\> ~/foo/  
NoMethodError: undefined method `~’ for /foo/:Regexp  
from (irb):2
