没有 else 的 Ruby 三元运算符
是否有“If do-this”和“do-this”的红宝石习惯用法只是作为一个简单的命令?
例如,我目前正在
object.method ? a.action : nil
将 else 子句留空,但我觉得可能有一种更惯用的方法来执行此操作,而无需在末尾指定 nil 。 (或者,我觉得在这种情况下占用多行代码会很浪费。
Is there a ruby idiom for "If do-this," and "do-this" just as a simple command?
for example, I'm currently doing
object.method ? a.action : nil
to leave the else clause empty, but I feel like there's probably a more idiomatic way of doing this that doesn't involve having to specify a nil at the end. (and alternatively, I feel like taking up multiple lines of code would be wasteful in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一般来说:在 Ruby 中你几乎不需要三元运算符。在 C 中之所以需要它,是因为在 C 中
if
是一个语句,所以如果你想返回一个值,你必须使用三元运算符,即一个表达。在 Ruby 中,一切都是一个表达式,没有任何语句,这使得三元运算符几乎是多余的。 在你的例子中,你总是可以
用
So 替换:
相当于
@Greg Campbell 指出的 反过来相当于尾随
if
修饰符形式另外,因为 Ruby 中的布尔运算符不仅仅返回 < code>true 或
false
,但是最后计算的表达式的值,您可以将它们用于控制流。这是从 Perl 导入的习惯用法,如下所示:As a general rule: you pretty much never need the ternary operator in Ruby. The reason why you need it in C, is because in C
if
is a statement, so if you want to return a value you have to use the ternary operator, which is an expression.In Ruby, everything is an expression, there are no statements, which makes the ternary operator pretty much superfluous. You can always replace
with
So, in your example:
is equivalent to
which as @Greg Campbell points out is in turn equivalent to the trailing
if
modifier formAlso, since the boolean operators in Ruby not just return
true
orfalse
, but the value of the last evaluated expression, you can use them for control flow. This is an idiom imported from Perl, and would look like this:Greg 的答案是最好的,但根据记录,甚至比 C 中的表达式和语句在 Ruby 中是等效的,所以除了
a.action if om?
你还可以做这样的事情:你可以写
(a; b; c) if d
或什至或就此而言:
(x; y; z) ? (a; bc) : (d; e; f)
Ruby 中不存在只允许单个语句或表达式的情况...
Greg's answer is the best, but for the record, and even more than in C, expressions and statements are equivalent in Ruby, so besides
a.action if o.m?
you can also do things like:You can write
(a; b; c) if d
or evenor for that matter:
(x; y; z) ? (a; b c) : (d; e; f)
There is no situation in Ruby where only a single statement or expression is allowed...
结果 = (<表达式> && <真值>) || <假值>
值 = 1
结果 = (值 == 1 && '一' ) || '二'
结果#=> '一'
解释一下: value == 1 && '一'#=>返回最后一个表达式结果,值等于 1,因此将对部分进行求值,并返回“one”。
<代码>值 = 0
结果 = (值 == 1 && '一' ) || '二'
结果#=> 'two'
解释:value != 1 和 'and' 表达式不会被求值,但 instad 会使用 'or' 表达式并返回 'two'
result = (<expression> && <true value>) || <false value>
value = 1
result = (value == 1 && 'one' ) || 'two'
result #=> 'one'
Explain: value == 1 && 'one' #=> returns last expression result, value is equals 1 so and section will be evaluated, and return 'one'.
value = 0
result = (value == 1 && 'one' ) || 'two'
result #=> 'two'
Explain: value != 1 and 'and' expression will not be evaluated, but instad will be used 'or' expression and it returns 'two'
在同一行上完成此操作的另一种方法是:
Rubocop 认为这是不好的风格,因为它使用分号来终止表达式,但我发现在某些情况下它比在末尾添加 if 语句更具可读性。更容易忽略最后的 if 语句,并且如果条件不为真,我并不总是想返回某些内容(因为您被迫使用三元运算符)。
你也可以更详细一些并且对 rubocop 更友好:
Another way this can be done on the same line is:
This is considered bad style by Rubocop because it uses a semicolon to terminate the expression, but I find it more readable in some conditions than tacking on the if statement at the end. It is easier to overlook an if statement at the end and I don't always want to return something if the condition isn't true(as you are forced into with a ternary operator).
You can also be a bit more verbose and rubocop friendly: