Is there any difference between these two statements:
! (name == "bob")
and
name != "bob"
To me, it seems like they both do the same thing. How are they different, if at all?
Is there any difference between these two statements:
! (name == "bob")
and
name != "bob"
To me, it seems like they both do the same thing. How are they different, if at all?
They are almost the same.
! (name == "bob")
calls two methods !
and ==
. you can write it like name.==('bob').!
.name != "bob"
calls only one method !=
. name.!=('bob')
.Unless you redefine !=
, you don't have to worry about these two options. In most cases, they are the same.
Here is an example how you can break it:
name = 'bob'
def name.!= s
true
end
name.!=('bob') # => true
name.==('bob').! # => false
You may expect the first result to be false
because the 'bob'
on the left side (variable name
) equals 'bob'
on the right side, but it does not because the method is redefined.
Those would return the same result in any example I could think of, but placing the !
before the expression can be helpful when checking other things (e.g. !my_array.include?("bob")
.