1.0 Classification

A quick recap

We've already covered the basics in our introduction to classes in Ruby. Here's a quick summary:

  • Classes act as templates for the creation of objects, specifying their state and behaviour
  • An object created by a class is called an instance of that class
  • A class is itself an object, and an instance of the class Class

Is it a thing?

When talking about objects, we often shorten "this object is an instance of the class User" to "this is a User." As far as we're concerned, both sentences mean the same thing and the phrase "is a" is equivalent to "instance of".

What's more, Ruby actually gives us a method called just that. Let's look at an example.

Example Code:

Output Window

As you've probably guessed, Object#is_a? accepts a single parameter - a class. Object#is_a? has an alias, Object#kind_of?. They're identical in every respect so you're free to pick whichever reads better in a given context.

Here's an exercise that requires you to combine Object#is_a? with something you've already learned about classes. As always, your objective is to make the tests pass.

Hint

Remember: Calling the method class on any object will give you its class.

Output Window

Congratulations, guest!


% of the book completed

or

This lesson is Copyright © 2011-2024 by Jasim A Basheer