if !

The ! operator returns a Bool that results from negating the truthyness of a value.

When used in an if in conjunction with a variable, is_a?, responds_to? or nil? the compiler will restrict the types accordingly:

  1. a = some_condition ? nil : 3
  2. if !a
  3. # here a is Nil because a is falsey in this branch
  4. else
  5. # here a is Int32, because a is truthy in this branch
  6. end
  1. b = some_condition ? 1 : "x"
  2. if !b.is_a?(Int32)
  3. # here b is String because it's not an Int32
  4. end