ruby_Ruby

ruby

Before we start with unless, let's see some basic things which we have already seen before. We are doing this because, it explains your question Why Unless? So, we see about or operator, if keyword and unless keyword. OR operator returns true, even if one of the condition is true.

在我们开始之前, unless ,我们先来看一些我们已经看过的基本知识。 我们这样做是因为,它解释了您的问题, 为什么不这样做 因此,我们看到about or operator, if关键字, unless关键字。 即使条件之一为真, OR运算符也会返回true。

if condition with logical or operator in Ruby

In the above code a variable weight is assigned with a value 32. Then a condition is checked using OR operator.

在上面的代码中,可变权重被赋值为32 。 然后使用OR运算符检查条件。

"You need to see a doctor". “您需要看医生”

Now, the first condition is checked weight < 35, this condition becomes true because the value of weight is 32 and OR operator doesn't check the second condition if the first condition is true. It simply returns true.

现在,检查第一个条件的权重<35 ,因为权重的值为32,并且如果第一个条件为true ,则OR运算符不会检查第二个条件,因此该条件变为true 。 它只是返回true。

When the condition is true, it displays the message "You need to see a doctor". Now, consider the value of weight as 87.

当条件为真时,它将显示消息“您需要看医生” 。 现在,将权重值视为87

if condition with logical or operator in Ruby

The first condition is checked weight < 35, the condition fails and it checks the second condition weight > 70, the condition is true, so the message is displayed. Now, what if the weight contains value between 35 and 70?

第一个条件检查的权重<35 ,该条件失败,并且它检查第二个条件的权重> 70 ,该条件为true,因此显示消息。 现在,如果权重值介于35到70之间怎么办?

if condition with logical or operator in Ruby

As usual, the first condition is checked, it fails and the second condition is checked and it also fails. Therefore, the puts statement is not executed.

通常,检查第一个条件,将其失败,然后检查第二个条件,并且也将失败。 因此,不执行puts语句。

When we talk about if keyword, it is used to make a decision, whether to execute the set of code or not. But, what if wanted to test the opposite of if? That is, you want to execute the code only when the condition is false. This is where unless comes into picture.

当我们讨论if keyword ,它用于决定是否执行代码集。 但是,如果要检验if的相反情况呢? 也就是说,您只想在条件为false时执行代码。 这是unless出现在图片中。

Unless Example in Ruby

This program displays the message "You're eligible to Vote" when the age is greater than 18. The value stored in the variable age is 23.

年龄大于18岁时,此程序将显示消息“您有资格投票” 。变量age中存储的值为23

When the condition is checked, it returns false and the puts statement is executed. This might be confusing.

检查条件时,它返回false并执行puts语句。 这可能会造成混淆。

Remember that unless is opposite of if.

You, can read this code statement as You're eligible to vote if your age is not less than 18. That is, it is similar to puts "You're eligible to Vote" if age > 18

如果您的年龄不小于18岁可以阅读此代码声明,因为您有资格投票 。 也就是说, puts "You're eligible to Vote" if age > 18 ,则类似于puts "You're eligible to Vote" if age > 18

If you're confused in reading the unless statement, simply read it in the opposite way of if statement.

如果您在阅读unless语句时感到困惑,只需以与if语句相反的方式阅读它。

翻译自: https://www.studytonight.com/ruby/unless-in-ruby

ruby

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值