Ruby regular expression method !~
Asked Answered
E

1

2

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.

Elah answered 22/12, 2012 at 8:25 Comment(0)
M
6

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.

Mccaleb answered 22/12, 2012 at 8:32 Comment(1)
Thanks for the right direction. It's a method of the Kernel module, which was mixined by the Object class.Elah

© 2022 - 2024 — McMap. All rights reserved.