Ruby:用于检查 nil /false 条件语句的干净代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17851723/
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
Ruby: Clean code for checking nil /false conditional statement?
提问by Kit Ho
I always meet this Ruby problem, I want to write it more cleanly.
我总是遇到这个Ruby问题,我想把它写得更干净。
var a can be nil
a.value can also be nil
a.value has possible true or false value
if (not a.nil?) && (not a.value.nil?) && a.value == false
puts "a value is not available"
else
puts "a value is true"
end
The problem is that the conditional statement is too clumsy and hard to read.
How can I improve the checking niland falseconditional statement?
问题是条件语句太笨拙且难以阅读。如何改进检查nil和false条件语句?
Thanks, I am a Ruby newbie
谢谢,我是 Ruby 新手
回答by nathanvda
Ruby on rails has an extension called trywhich allows you to write:
Ruby on rails 有一个名为的扩展try,它允许您编写:
if a.try(:value) == false
which is very clean. Without try, you can just write
这是非常干净的。没有try,你可以只写
if a && a.value == false
If a.valueis nil, it is not false, so that is ok :)
如果a.value是零,它不是假的,所以没关系:)
If it is possible that a.valueis not defined (which would raise an exception), I would write that as follows:
如果可能a.value没有定义(这会引发异常),我会写如下:
if a && a.respond_to?(:value) && a.value == false
[UPDATE: after ruby 2.3]
[更新:在 ruby 2.3 之后]
Since ruby 2.3 there is an even shorter version:
从 ruby 2.3 开始,还有一个更短的版本:
if a&.value == false
which is almost equivalent to a.try(:value)(but is pure ruby). Differences:
这几乎相当于a.try(:value)(但纯红宝石)。区别:
- if
valuedoes not exist, the&.operator will throw,trywill just return nil (preferable or not?)(note:try!would also throw). - when cascading
tryor&.they also handlefalsedifferently. This follows logically from previous difference,trywill return nil, while&.will throw becausefalseknows no methods :P
- 如果
value不存在,&.运算符将抛出,try将只返回 nil(最好还是不存在?)(注意:try!也会抛出)。 - 级联时,
try或者&.它们的处理false方式也不同。这在逻辑上遵循先前的差异,try将返回 nil,而&.将抛出,因为不false知道任何方法:P
回答by Tegar
You can achieve it in more compacted way using Safe Navigation Operator (&.):
if a&.value == false
您可以使用 Safe Navigation Operator ( &.)以更紧凑的方式实现它:
if a&.value == false
Source : http://mitrev.net/ruby/2015/11/13/the-operator-in-ruby/
回答by Salil
if a && a.value!=false
puts "a value is true"
else
puts "a value is not available"
end
or just
puts a && a.value!=false ? "a value is true" : "a value is not available"
回答by MCB
The simplest and cleanest way is to flip it and reverse it. Check for the truthyvalue rather than the falseyvalues
最简单和最干净的方法是翻转它并反转它。检查truthy值而不是falsey值
if a && a.value
puts "a value is true"
else
puts "a value is not available"
end
Of course in Railsyou could do it either way by using blank?or present?
当然,Rails您可以通过使用blank?或present?
回答by sawa
Your condition is redundant. If a.valueis to be false, then it would not be nil.
你的条件是多余的。如果a.value是false,那就不会了nil。
if a.nil?.! && a.value == false
puts "a value is not available"
else
puts "a value is true"
end
回答by levelone
This will always return a boolean, if nil it will return false; if false it returns false; if true returns true. Try it out, it's nice and short:
这将始终返回一个布尔值,如果为零则返回 false;如果为假,则返回假;如果真返回真。试试看,它很好而且很短:
!!a.try(:value) == false
!!a.try(:value) == false

