没有 else 的 Ruby 三元运算符 [英] Ruby ternary operator without else

查看:25
本文介绍了没有 else 的 Ruby 三元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

If do-this"和do-this"是否有一个简单的命令?

Is there a ruby idiom for "If do-this," and "do-this" just as a simple command?

例如,我目前正在做

object.method ? a.action : nil

将 else 子句留空,但我觉得可能有一种更惯用的方式来执行此操作,而无需在末尾指定 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.

推荐答案

a.action if object.method?

这篇关于没有 else 的 Ruby 三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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