A brief introduction to Classes, Objects, and how we work together with them…

Object-Oriented-Programming (OOP), in my opinion, is an often misunderstood and overlooked concept for those beginning to learn development. Sure, I know how variables work and how to write some cool and useful functions/ methods, but what is an object and why is it important? That’s a question I found myself asking quite often while learning the basics of Ruby and it wasn’t long before I found my answer. Let’s start by taking a look at some of the key components of OOP.

Classes, Objects, Instances, and Methods

As a developer, you’ll often be tasked to model your code after real-world objects, events, and scenarios. Think of a house for example.

Every house that’s built must have a blueprint to tell the constructors how to build the house. In development, that’s where our Class comes in. You can think of a class as a blueprint that holds the basic information about what our house should look like and you can think of the individual houses made using that blueprint as an object. Methods are instructions to how we manipulate and interact with classes and how they interact with each other.

Something to note is that everything in Ruby is an object. Every string, integer, array, hash, etc. that you create are all instances of their parent class!

For example, if you were to assign a variable “greet” to the value “Hello World” and call the .class method, you’d see this:

greet = "Hello World"greet.class
=> String

Ruby is letting us know that greet which is set to "Hello World" belongs to the string Class and therefore has all the associated string methods at its disposal.

Now that we have an idea of what a class is, let’s make one!

class Houseend

Not too difficult, right? What we’ve just done is told our program that there is a new Class called “House.” That’s not very exciting or useful in its current state, so why don't we add a few instructions! All houses should at least have bedrooms, bathrooms, and some square footage, right?

class House 
def initialize(bedrooms, bathrooms, sq_foot)
@bedrooms = bedrooms
@bathrooms = bathrooms
@sq_foot = sq_foot

Now we’ve taken the House class and added a few instructions. You see that we add a method called initialize to the class, and this method accepts a few arguments, bedrooms, bathrooms, sq_foot. We’ve also assigned those arguments to instance variables within the method which I’ll come back to later. What’s important for now is that initialize method and what it means to Ruby.

Whenever you call the method new on a class(i.e. House.new) the class will create a new instance of itself. It will also call the method initialize on the new object. Doing so it will simply pass all the arguments that you passed to new on to the method initialize.

Now that we have created our House class, let’s make our first House object. To do that, we need to call the .new method as mentioned above and pass in our arguments that are specific to our new house. You’ll often want to be able to use any instances of a class that you create so it’s important to assign them to a variable for later reference. my_house = House.new(4, 2, 2300) will create a new instance of a house with 4 bedrooms, 2 bathrooms, and 2300 sq_feet and assign it to the variable, my_house, giving you the ability to refer back to it at any point. Just like we did with our string class earlier, we can now use the .class method to check our my_house variable and see that it’s class, sure enough, is House.

my_house = House.new(4, 2, 2300)
=> House

We’ve just created our first Class and initialized an instance of that class with the .new method! Sweet! Now that you know how to create a class and give its instances defining and unique values, you’re well on your way to mastering OOP.

Software Developer