没有 else 的 Ruby 三元运算符

发布于 2024-08-19 23:47:02 字数 208 浏览 9 评论 0原文

是否有“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我一直都在从未离去 2024-08-26 23:47:02

一般来说:在 Ruby 中你几乎不需要三元运算符。在 C 中之所以需要它,是因为在 C 中 if 是一个语句,所以如果你想返回一个值,你必须使用三元运算符,即一个表达。

在 Ruby 中,一切都是一个表达式,没有任何语句,这使得三元运算符几乎是多余的。 在你的例子中,你总是可以

cond ? then_branch : else_branch

if cond then then_branch else else_branch end

So 替换:

object.method ? a.action : nil

相当于

if object.method then a.action end

@Greg Campbell 指出的 反过来相当于尾随 if 修饰符形式

a.action if object.method

另外,因为 Ruby 中的布尔运算符不仅仅返回 < code>true 或 false,但是最后计算的表达式的值,您可以将它们用于控制流。这是从 Perl 导入的习惯用法,如下所示:

object.method and a.action

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

cond ? then_branch : else_branch

with

if cond then then_branch else else_branch end

So, in your example:

object.method ? a.action : nil

is equivalent to

if object.method then a.action end

which as @Greg Campbell points out is in turn equivalent to the trailing if modifier form

a.action if object.method

Also, since the boolean operators in Ruby not just return true or false, 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:

object.method and a.action
南薇 2024-08-26 23:47:02
a.action if object.method?
a.action if object.method?
一页 2024-08-26 23:47:02

Greg 的答案是最好的,但根据记录,甚至比 C 中的表达式和语句在 Ruby 中是等效的,所以除了 a.action if om? 你还可以做这样的事情:

object.method? && a.action

你可以写 (a; b; c) if d 或什至

(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:

object.method? && a.action

You can write (a; b; c) if d or even

(a
 b
 c
) if d

or 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...

装迷糊 2024-08-26 23:47:02

结果 = (<表达式> && <真值>) || <假值>

值 = 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'

滥情空心 2024-08-26 23:47:02

在同一行上完成此操作的另一种方法是:

if object.method; a.action end

Rubocop 认为这是不好的风格,因为它使用分号来终止表达式,但我发现在某些情况下它比在末尾添加 if 语句更具可读性。更容易忽略最后的 if 语句,并且如果条件不为真,我并不总是想返回某些内容(因为您被迫使用三元运算符)。

你也可以更详细一些并且对 rubocop 更友好:

if object.method then a.action end

Another way this can be done on the same line is:

if object.method; a.action end

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:

if object.method then a.action end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文