Reputation: 118271
I was trying to play with the operators !~
and !=
in below code. But couldn't figure out such any differences. But I have doubt, If not so, why Ruby introduced them?
2 !=3
# => true
2 !~ 3
# => true
c= [1,2,3]
# => [1, 2, 3]
d=[1,4,5]
# => [1, 4, 5]
c != d
# => true
c !~ d
# => true
Could anyone please help me here by saying if any difference between them ?
Upvotes: 3
Views: 517
Reputation: 118691
The =~
operator and its negative !~
are for pattern-matching. It is overridden by Regexp and String to provide regular-expression pattern matching, but for numbers it is not implemented. This is why 2 =~ 3
gives nil
, so 2 !~ 3
is true
.
Upvotes: 7