コンテンツへスキップ

if !

! 演算子は、値の真偽値を否定した結果の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