Ruby on Rails 中是否有 if(没有 else)语句的简写? [英] Is there a shorthand if (without else) statement in Ruby on Rails?

查看:49
本文介绍了Ruby on Rails 中是否有 if(没有 else)语句的简写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Ruby 中有一个简写的单行 if/else 语句:

I know there is a shorthand one-line if/else statement in Ruby:

a ? b : c

是否只有一个 if 语句?而不是写这个:

Is there one for just a single if statement? Instead of writing this:

if a
  # do something
end

有简写版本吗?

推荐答案

你可以使用 post 条件(不要介意名字,它会被评估before代码.并且 do_something 仅在条件计算为真值时才会执行(即不是 nilfalse)).

You can use post conditions (don't mind the name, it will be evaluated before the code. And do_something will only be executed if condition evaluates to truthy value (i.e. not nil or false)).

do_something if a

这篇关于Ruby on Rails 中是否有 if(没有 else)语句的简写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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