检查是否为假然后运行代码 [英] Check if false then run code

查看:43
本文介绍了检查是否为假然后运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测试一些东西.如果 false 这样做.我不想测试真,只测试假.我的代码看起来像这样,如果是假的,我只需要做一些事情:

I want to test something. If false do this. I wish not to test for true, only false. My code looks like this and I only need to do something if false:

if Model.exists?(table_column: 'joe')
  # this part is blank
else
# run this code
end

我的代码在我想要的 else 之后运行.是否有一个干净的 rails/ruby 方法来测试错误?我的代码只会在 false 时运行,而不是 true.

My code runs after the else which is what I want. Is there a clean rails/ruby method that tests for false? My code would only run if false, not true.

如果重复,我很乐意删除.谢谢

If duplicate, I'll gladly delete. Thanks

推荐答案

使用 unless

unless Model.exists?(table_column: 'joe')
  # run code
end

如果不是

if not Model.exists?(table_column: 'joe') 
  # run code
end

if Model.find_by(table_column: 'joe').blank?
  # run code
end

希望有帮助!

这篇关于检查是否为假然后运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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