Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if string is not equal to either of two strings

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?

like image 309
PropSoft Avatar asked Jun 01 '13 08:06

PropSoft


People also ask

How do you check if a string is not equal to another string?

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.

How do you compare whether two strings are equal or not?

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.

Can you use != For strings in Java?

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.

Can you use == on 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.


1 Answers

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" %>
like image 111
Old Pro Avatar answered Oct 06 '22 15:10

Old Pro