Ruby-on-rails 我们是否将 else 与除非语句一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38627935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do we use else with unless statement?
提问by Waleed Arshad
There exists an opinion about elsestatement that we should not use it with unless?
存在关于else我们不应将其与unless?
Can anybody explain why this is so, or are we free to do whatever we like?
任何人都可以解释为什么会这样,或者我们可以自由地做我们喜欢做的事吗?
回答by Mureinik
You definitely canuse elsewith unless. E.g.:
你绝对可以使用elsewith unless。例如:
x=1
unless x>2
puts "x is 2 or less"
else
puts "x is greater than 2"
end
Will print "x is 2 or less".
将打印“x 为 2 或更少”。
But just because you cando something doesn't mean you should. More often than not, these constructs are convoluted to read, and you'd be better served to phrase your condition in a positive way, using a simple if:
但是,仅仅因为您可以做某事并不意味着您应该这样做。通常情况下,这些结构读起来令人费解,您最好使用简单的方式以积极的方式表达您的病情if:
x=1
if x<=2
puts "x is 2 or less"
else
puts "x is greater than 2"
end
回答by sohail khalil
Yes, we can use unlesswith else. We are free to go wherever we like but some people have opinion that it's not a good habit to use elsewith unless. We need to avoid from this.
是的,我们可以使用unlesswith else。我们可以自由地去的地方,我们喜欢,但有些人认为这不是一个好习惯使用else同unless。我们需要避免这种情况。
Unfortunately, unless statement supports only else construct not elsif or elseunless with in it.
不幸的是,除非语句只支持 else 构造而不支持 elsif 或 elseunless with。
unless true
puts "one"
elsif true
puts "two"
else
puts "three"
end
SyntaxError: compile error
syntax error, unexpected kELSIF, expecting kEND
This may also be the reason that it restrict us.
这也可能是它限制我们的原因。
回答by Steve Jessop
Ruby allows it, but in English"if ... else" (or "if ... otherwise") is a common construction whereas "unless ... else" is pretty much unheard of, to the point where many native speakers would reject it as incorrect.
Ruby 允许这样做,但在英语中,“if ... else”(或“if ... else”)是一种常见的结构,而“除非 ... else”几乎闻所未闻,以至于许多母语人士都会拒绝它是不正确的。
This results in some confusion with unless/elsein Ruby. People whose native language doesn't allow it just aren't used to following it. That's why there are many opinions against it. Since Ruby supports it you are of course free to do it anyway.
这会导致与unless/elseRuby 的一些混淆。母语不允许的人只是不习惯遵循它。这就是为什么反对它的意见很多。由于 Ruby 支持它,因此您当然可以随意进行。
回答by Santhosh
There is nothing stopping anyone from using unless - else. It is perfectly valid. But the else part of unless-else is a double negative.
没有什么能阻止任何人使用unless - else. 这是完全有效的。但是unless-else 的else 部分是双重否定。
In some cases, unlessis easy to comprehend. But when combined with an elsestatement, if - elseis always easierto comprehend than unless - else. Imagine trying to understand the else part of an unless - elsecondition. It is a double negative and double negatives is just very not not wrong.
在某些情况下,unless很容易理解。但是当与else语句结合使用时,if - else总是比更容易理解unless - else。想象一下试图理解unless - else条件的 else 部分。这是一个双重否定,双重否定是非常正确的。
Also if there are many conditions with combinations of &&and ||operators, unlessbecomes more difficult to comprehend
此外,如果有很多条件与&&和||运算符的组合,则unless变得更难以理解
回答by hgsongra
回答by spickermann
Technically it is perfectly fine to use an unlesswith an else:
从技术上讲,将 anunless与 an一起使用是完全没问题的else:
unless user.staff?
# do foo
else
# do bar
end
The only problem is that it is sometimes hard to read and to understand. Since code is way more often read than written, it makes much sense to focus on readablity and to write code that is easy to understand for other developers in the future.
唯一的问题是有时难以阅读和理解。由于代码的阅读频率远高于编写频率,因此关注可读性并编写未来其他开发人员易于理解的代码是非常有意义的。
Therefore you might want to change it to:
因此,您可能希望将其更改为:
if !user.staff?
# do bar
else
# do foo
end
Or even better make remove the negation from the condition completely:
或者甚至更好地从条件中完全删除否定:
if user.customer?
# do bar
else
# do foo
end
if trueis just easier to understand and simpler to parse in you head than if !trueor unless true. Dealing with negations (or sometimes even double negations) makes you think twice. Therefore the community tries to avoid it.
if true只是比if !trueor更容易理解和更容易在你的头脑中解析unless true。处理否定(有时甚至是双重否定)会让你三思而后行。因此,社区试图避免它。
But technically it is fine...
但从技术上讲是好的...
回答by zwol
It might be appropriate (more readable than any alternative) to use unless ... else ...when the code in the elseblock is very long and the code in the unlessblock is very short.
unless ... else ...当else块中的代码很长而块中的代码unless很短时,使用它可能是合适的(比任何替代方案都更具可读性)。
unless user.staff?
raise SecurityError, 'not authorized'
else
# ...
# ...
# ... lots of code
# ...
# ...
end
If you wrote this the other way around,
如果你反过来写,
if user.staff?
# ...
# ...
# ... lots of code
# ...
# ...
else
raise SecurityError, 'not authorized'
end
the else-clause might be so far removed from its condition that you'd be making people scroll up and down to keep things straight in their heads.
else 子句可能与其条件相去甚远,以至于您会让人们上下滚动以保持头脑清醒。
This particular exampleis better written without an elseat all...
这个特殊的例子写得更好,完全没有else......
unless user.staff?
raise SecurityError, 'not authorized'
end
# ...
# ...
# ... lots of code
# ...
# ...
... but that transformation is not always possible.
......但这种转变并不总是可能的。
回答by Umar Khan
Executes code if conditional is false. If the conditional is true, code specified in the else clause is executed.
如果条件为假,则执行代码。如果条件为真,则执行 else 子句中指定的代码。
x=1
unless x>2
puts "x is less than 2"
else
puts "x is greater than 2"
end
We can use else with unless but its a bad approach.
我们可以使用 else 和除非,但这是一种不好的方法。

