I am just learning RoR so please bear with me. I am trying to write an if or statement with strings. Here is my code:
<% if controller_name != "sessions" or controller_name != "registrations" %>
I have tried many other ways, using parentheses and ||
but nothing seems to work. Maybe its because of my JS background...
How can I test if a variable is not equal to string one or string two?
Use the strict inequality (! ==) operator to check if two strings are not equal to one another, e.g. a !== b . The strict inequality operator returns true if the strings are not equal and false otherwise.
The equals() method compares two strings, and returns true if the strings are equal, and false if not. Tip: Use the compareTo() method to compare two strings lexicographically.
Note: When comparing two strings in java, we should not use the == or != operators. These operators actually test references, and since multiple String objects can represent the same String, this is liable to give the wrong answer. Instead, use the String.
You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.
This is a basic logic problem:
(a !=b) || (a != c)
will always be true as long as b != c. Once you remember that in boolean logic
(x || y) == !(!x && !y)
then you can find your way out of the darkness.
(a !=b) || (a != c)
!(!(a!=b) && !(a!=c)) # Convert the || to && using the identity explained above
!(!!(a==b) && !!(a==c)) # Convert (x != y) to !(x == y)
!((a==b) && (a==c)) # Remove the double negations
The only way for (a==b) && (a==c) to be true is for b==c. So since you have given b != c, the if
statement will always be false.
Just guessing, but probably you want
<% if controller_name != "sessions" and controller_name != "registrations" %>
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