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 …

nil.class

and it returns NilClass

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

So

nil == nil

is always true.

How to determine if an object is does not exist?

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