Zero is not nil

I’m learning the Ruby scripting language while learning Ruby on Rails. One of the constants of Ruby is that everything is an object.

I was just coding in Objective C and one of the cool things you can do is call a method on a nil object and it just returns nil. This is nice because you don’t have to go checking for nil every time you get an object before calling a method.

So I got to wondering, in Ruby is the integer object 0 nil? So I wrote a quick Ruby program to see.


if 0.nil?
puts "true"
else
puts "false"
end

This outputs false because 0 is not a nil object. There is an object called nil so


if nil.nil?
puts "true"
else
puts "false"
end

is valid code and it outputs true.