Bummer! This is just a preview. You need to be signed in with a Treehouse account to view the entire video.
Nil3:55 with Jason Seifer
Ruby gives us one more boolean kind of value to work with called nil. While true and false mean that something is returned,
nil is the absence of anything. Just like
nil is the only instance of
NilClass, which makes it a Singleton as well.
nil.class nil.to_i nil.to_a nil.to_h nil.nil?
Implicit return of nil:
class MyClass def my_method end end MyClass.new.my_method.inspect
Ruby gives us one more Boolean kind of value to work with called nil.
While true and false mean that something is returned,
nil is the complete absence of anything.
Just like true and false, nil is the only instance of nil class,
which makes it a singleton as well.
Let's take a look at nil now, using work spaces.
Okay, so here we are inside of work spaces.
And let's launch irb, and take a look at working with nil.
So if we just take a look at nil itself, it returns nil.
And just like true and false which are instances of TrueClass and
FalseClass, nil is the one and only instance of NilClass.
Nil is also a singleton.
Now nil is also pretty interesting if we look at the NilClass documentation
we can see on the left here we have a few
different methods that can be called on nil.
So let's go ahead and take a look at those.
First, let's inspect nil.
This returns the string nil which can be very useful if
we want to print it out because since nil is the absence of anything,
if we try to print that out somewhere, we wouldn't get anything.
So the inspect method prints out the string nil.
If we convert nil to an integer it is zero, but
If we ask zero if it's nil, that returns false.
The same thing is true with to a.
To convert nil to an array, it returns an empty array.
But an empty array is not nil.
However, an empty array is equal
to nil converted into an array.
Nil to a hash returns an empty hash.
And we can even ask nil if it is nil.
In fact, you can ask almost any object in Ruby If it's nil like an integer,
a string, or even the true and false classes.
And these all return false because they are something.
Nil can also be kind of falsey by itself.
If we use the not operator on nil that returns true.
So, nil is kind of like being false however,
false has a tiny bit higher of precedence than nil.
And if you wanted to see any of those
printed out you can open up nil.rb.
And that will show you pretty much everything that we just went over,
inside of IRB.
Now one thing that you won't see printed out is nil to an array, and the reason for
that is that when empty arrays are printed to the screen,
nothing is actually printed.
So let's close that and open up myclass.rb.
Now, remember Ruby has something call implicit returns.
That means, that the value of the last line of any method call
is returned from that method.
So, if we don't have anything inside of a method body nil is returned.
And if we were to run this, we can see that it prints out nil.
You need to sign up for Treehouse in order to download course files.Sign up