It may seem like a boring topic, because in this section we’re literally talking about the concept of nothing. PHP’s null and Ruby’s nil may seem similar at surface level because they both represent the idea of an empty variable.

However, Ruby’s nil is not just a placeholder for empty variables - often it is used as a way to signal application controls. More on that in a bit. First, we need to cover the basics of PHP’s null:

null in PHP

PHP’s null is a scalar datatype, meaning that it’s a simple data structure and not an object. This is just common sense.

You can test for null in a couple of ways. The easiest to understand is the function isnull($thing). This function will just return a Boolean that represents if the $thing is null or not:

$thing = null;

if(isnull($thing)) {
  echo 'yup $thing is definitely not a thing';
}

=> 'yup $thing is definitely not a thing'

Pretty simple.

Nil in Ruby

That wasn’t a typo, I’ve capatilized Nil in the title of this section. That’s because like everything else in Ruby - the concept of nothing is an object. That’s right - Nil with a capital N is an object in Ruby.

In fact there’s an entire page of documentation on the NilClass on the Ruby website.

But unlike in PHP where you have global functions lying about at your disposal whenever you need to do a common operation like checking if a variable is nil; instead you’re given methods on the object itself.

To check to see if an object is Nil in Ruby, we have a method on Nil itself for that:


thing = nil

if thing.nil?
  puts 'yup thing is definitely not a thing'
end

=> 'yup thing is definitely not a thing'

Coming from a PHP background, this may seem bonkers. How could every object in a lanuage possibly implement the .nil? method? The answer is that every class in Ruby inherits from a Class called Object. Seriously.

If you look at the Object Class’s Documentation on the ruby-doc website you’ll see that it implements a variety of methods, one such key method is .nil?.

Since everything inherits from Object, therefore everything implements .nil?. In an way it’s the equivalent of a global function.

Truthy or Falsey

In the beginning I hinted that Ruby’s nil was a little more than just a signal that a particular variable was empty. It’s also a way of signifing that a variable is falsey.

Falsey means that a value is not truely false , but if passed to a conditional then it’s treated as if it was one.

So nil is not actually false. They’re 2 different concepts. However, to a conditional, they both mean the same thing.

Sometimes examples speak louder than descriptions. Check these out:


thing = false

if !thing
  puts 'thing is Falsey'
end

=> 'thing is Falsey'

That’s expected right? Let’s try it with nil:


thing = nil

if !thing
  puts 'thing is Falsey'
end

=> 'thing is Falsey'

Same behavior! Ruby is somewhat different in that almost everything is truthy:

  • '1' is truthy
  • '0' is truthy
  • 1 is truthy
  • 0 is truthy
  • 'any string' is truthy

However there are only 2 structures in Ruby that are falsey:

  • false is falsey
  • nil is falsey

Compared to PHP this is a simple concept. In order to know if a particular data structure is truthy or falsey in PHP you can to consort a data type that defines under what conditions a variable is truthy vs falsey.

Here’s the official PHP data type comparisons table

So, long story short. Ruby’s nil sounds complicated because it’s in fact an object. However, it plays into Ruby’s OOP nature and it provides a guessable path to understanding. The principle of least surprise holds up well here.