if !
¶
!
运算符返回一个 Bool
,该 Bool
是对一个值的 真值 取反的结果。
当在 if
中与变量、is_a?
、responds_to?
或 nil?
结合使用时,编译器将相应地限制类型
a = some_condition ? nil : 3
if !a
# here a is Nil because a is falsey in this branch
else
# here a is Int32, because a is truthy in this branch
end
b = some_condition ? 1 : "x"
if !b.is_a?(Int32)
# here b is String because it's not an Int32
end