差异 - 除非/如果 [英] Difference - unless / if

查看:32
本文介绍了差异 - 除非/如果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下 ifunless 之间的区别以及何时使用它?

Can anyone explain me the difference between if and unless and when to use it?

推荐答案

unless 只是一个否定的 if.也就是说,如果条件为真,它会执行它包含的任何内容.

unless is just a negated if. That is, it executes whatever it contains if the condition is not true.

unless foo?
    # blabla
end

简单的意思

if !foo?
    # blabla
end

这完全取决于你觉得什么更容易阅读,真的.

It's all a matter of what you find easier to read, really.

另见:除非,被滥用的 Ruby 条件

这篇关于差异 - 除非/如果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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