I found this operator by chance:
ruby-1.9.2-p290 :028 > "abc" !=~ /abc/
=> true
what's this? It's behavior doesn't look like "not match".
In order to compare things Ruby has a bunch of comparison operators. The operator == returns true if both objects can be considered the same. For example 1 == 1 * 1 will return true , because the numbers on both sides represent the same value.
The === (case equality) operator in Ruby.
Triple Equals Operator (More Than Equality) Our last operator today is going to be about the triple equals operator ( === ). This one is also a method, and it appears even in places where you wouldn't expect it to. Ruby is calling the === method here on the class.
Two strings or boolean values, are equal if they both have the same length and value. In Ruby, we can use the double equality sign == to check if two strings are equal or not. If they both have the same length and content, a boolean value True is returned. Otherwise, a Boolean value False is returned.
That's not one operator, that's two operators written to look like one operator.
From the operator precedence table (highest to lowest):
[] []=
**
! ~ + -
[unary]
[several more lines]<=> == === != =~ !~
Also, the Regexp class has a unary ~
operator:
~ rxp → integer or nil
Match—Matchesrxp
against the contents of$_
. Equivalent torxp =~ $_
.
So your expression is equivalent to:
"abc" != (/abc/ =~ $_)
And the Regexp#=~
operator (not the same as the more familiar String#=~
) returns a number:
rxp =~ str → integer or nil
Match—Matches rxp against str.
So you get true as your final result because comparing a string to a number is false.
For example:
>> $_ = 'Where is pancakes house?'
=> "Where is pancakes house?"
>> 9 !=~ /pancakes/
=> false
>> ~ /pancakes/
=> 9
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With