What is ruby nil?

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

nil is ruby’s singleton object.  It is an instance of the NilClass which you can see when you do …


and it returns NilClass

Because nil is an singleton, there is always exactly one instance of the NilClass and that is nil.


nil == nil

is always true.

How to determine if an object is does not exist?

if foobar.nil?
puts “No foobar”
puts “Has foobar”