测试string是否不等于两个字符串中的任何一个
我只是在学习RoR所以请耐心等待。 我试图用字符串写一个if或语句。 这是我的代码:
我已经尝试了许多其他方法,使用括号和||
但似乎没什么用。 也许是因为我的JS背景……
如何测试变量是否不等于字符串1或字符串2?
这是一个基本的逻辑问题:
(a !=b) || (a != c)
只要b!= c,它将永远为真。 一旦你在布尔逻辑中记住它
(x || y) == !(!x && !y)
那么你就可以找到摆脱黑暗的道路。
(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
(a == b)&&(a == c)为真的唯一方法是b == c。 因此,既然你已经给出了b!= c,那么if
语句将始终为false。
只是猜测,但可能你想要
<% if controller_name != "sessions" and controller_name != "registrations" %>
<% unless ['sessions', 'registrations'].include?(controller_name) %>
要么
<% if ['sessions', 'registrations'].exclude?(controller_name) %>