Вопрос

I don't remember where I learned the !~ method of the String class. However I know it compares a string to a regex and check whether the string not match the regex. See my below example.

C:\>irb
irb(main):001:0> "abba" =~ /(\w)(\w)\2\1/i
=> 0
irb(main):002:0> "xxxx" =~ /(\w)(\w)\2\1/i
=> 0
irb(main):003:0> "asdf" =~ /(\w)(\w)\2\1/i
=> nil
irb(main):004:0> "asdf" !~ /(\w)(\w)\2\1/i
=> true
irb(main):005:0> "asdf" !~ /asdf/i
=> false
irb(main):006:0>

I want to find more information of the method but I can't find it in the rdoc of both String and Regexp. Anyone can give some help?

Thanks.

Это было полезно?

Решение

Since this is the method you can find it here in the Methods filter. I've found this description.

obj !~ other → true or false

Returns true if two objects do not match (using the =~ method), otherwise false.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top