检查变量在 ruby​​ 中是否不为零且不为零 [英] Checking if a variable is not nil and not zero in ruby

查看:33
本文介绍了检查变量在 ruby​​ 中是否不为零且不为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来检查变量是否不为零且不为零

I am using the following code to check if a variable is not nil and not zero

if(discount != nil && discount != 0) 
  ...
end

有没有更好的方法来做到这一点?

Is there a better way to do this?

推荐答案


unless discount.nil? || discount == 0
  # ...
end

这篇关于检查变量在 ruby​​ 中是否不为零且不为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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