Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the !~ method do with String in Ruby

Tags:

ruby

From @sawa's answer at: https://stackoverflow.com/a/21892359/226255

What does !~ do?

Example:

re = /[^\d.,]/
"0.0687987167581341,0.120311605902415,89.8399554017928,198.151088713489" !~ re

I couldn't find any documentation in String or Regexp

like image 332
Abdo Avatar asked Sep 02 '25 17:09

Abdo


1 Answers

The method !~ is the inverse of =~, that is !(=~). From the Ruby Object#!~ documentation:

[obj !~ other ] returns true if two objects do not match (using the =~ method), otherwise false.

So, since String#=~ performs a string/regex match returning the index of the first match if matched and nil otherwise, String#!~ return false if matched and true otherwise.

like image 68
mdesantis Avatar answered Sep 05 '25 06:09

mdesantis