nil
In this page:
nil
nil represents the absence of a value in Ruby, similar to null in other languages, and is an instance of NilClass. Uninitialized instance variables default to nil, and many methods return nil to signal "nothing found". nil.to_s gives an empty string and nil.to_a gives an empty array, which can be convenient in some contexts.
Warning: Calling most methods on nil (other than the ones NilClass itself defines) raises a NoMethodError -- always check for nil before use if it's possible.
Example: nil
value = nil
puts value.nil?
puts value.class
puts value.to_s.empty?
value = "now set"
puts value.nil?
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: