1

What is the purpose of the exclamation mark right before the taking_damage method in this example of code?

if warrior.health < 20 && !taking_damage?(warrior)
     warrior.rest!
end
4

3 回答 3

7

It is the boolean operation Negation and thus equivalent to the Ruby keyword not.

You can read the line like this:

“If the warrior's health is below 20 and the warrior is NOT taking damage then the warrior must rest.”</p>

于 2014-02-22T05:10:31.593 回答
2

It is the negation operator which takes the method/variable's truthiness and returns the opposite of it.

于 2014-02-22T05:10:58.337 回答
1

此运算符还可用于将任何值转换为真假(布尔值)。这是一个例子:

 > !nil
 => true 
 > !!nil
 => false 
于 2014-02-22T20:23:57.867 回答