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

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

问题描述

是否有一个简单的命令就可以做到如果做这个"和做这个"的红宝石成语?

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?

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

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