测试string是否不等于两个字符串中的任何一个 [英] Test if string is not equal to either of two strings

查看:736
本文介绍了测试string是否不等于两个字符串中的任何一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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" %>

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

I have tried many other ways, using parentheses and || but nothing seems to work. Maybe its because of my JS background...

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

How can I test if a variable is not equal to string one or string two?

推荐答案

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

This is a basic logic problem:

(a !=b) || (a != c) 

只要b!= 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

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

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" %>

这篇关于测试string是否不等于两个字符串中的任何一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆