测试string是否不等于两个字符串中的任何一个

Pro*_*oft 15 ruby logic if-statement

我只是在学习RoR所以请耐心等待.我试图用字符串写一个if或语句.这是我的代码:

<% if controller_name != "sessions" or controller_name != "registrations" %>
Run Code Online (Sandbox Code Playgroud)

我尝试了许多其他方法,使用括号,||但似乎没有任何工作.也许是因为我的JS背景......

如何测试变量是否不等于字符串1或字符串2?

zol*_*ter 15

<% unless ['sessions', 'registrations'].include?(controller_name) %>
Run Code Online (Sandbox Code Playgroud)

要么

<% if ['sessions', 'registrations'].exclude?(controller_name) %>
Run Code Online (Sandbox Code Playgroud)


Old*_*Pro 14

这是一个基本的逻辑问题:

(a !=b) || (a != c) 
Run Code Online (Sandbox Code Playgroud)

只要b!= c,它将永远为真.一旦你在布尔逻辑中记住它

(x || y) == !(!x && !y)
Run Code Online (Sandbox Code Playgroud)

那么你就可以找到摆脱黑暗的道路.

(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
Run Code Online (Sandbox Code Playgroud)

(a == b)&&(a == c)为真的唯一方法是b == c.因此,既然你已经给出了b!= c,那么该if语句将始终为false.

只是猜测,但可能你想要

<% if controller_name != "sessions" and controller_name != "registrations" %>
Run Code Online (Sandbox Code Playgroud)