nil
nil value, nil object
The nil value is used as a concept to express the meaning of “the absence of value” or “no object” or “a missing object” or “a lack of an object”.
In other programming languages it is often called null.
Like any other value in Ruby, nil is an object. It is an object of the class NilClass.
myarray = []
myarray[0] # => nil
nil.class # => NilClass
nil? method
The nil? method is defined for any object.
All objects except nil return false.
You can use this method to test if an object is nil.
false.nil? # => false
0.nil? # => false
nil.nil? # => true
myarray = []
myarray[0] == nil # => true
myarray[0].nil? # => true
boolean expressions
Inside a boolean expression nil is treated as false.
In Ruby false and nil are the only values (objectes) to be considered “falsy”. Any other value (object) is considered to be “truthy”.
!! nil # => false
not not nil # => false
myarray = []
myarray[0] ? 'Element at index 0 exists' : 'There is no element at index 0' # => "There is no element at index 0"