How Rubyists Use Abstraction

A Brief Introduction to Object-Orientation

Lauren Cunningham
The Startup
4 min readSep 25, 2020

--

Photo by RF._.studio from Pexels

Even if you’ve never heard of object-orientation, I’m sure you have heard of the term ‘abstraction’. Today, I want to share some insights on what these terms mean in the world of programming and how we use them as tools to make our code both beautifully simplistic and effective.

From Grady Booch on SlidePlayer

Given the definition of abstraction, you can probably think of an example of this in everyday life. This is something that represents the idea of an ‘object’.

Think about when you clicked this link on your smartphone… is that a real button? No, it’s an abstraction of a “real” button. Isn’t a button just an abstraction for action as well? When you click on a link, you’re not really physically touching or opening anything, it’s an abstraction!

If you’ve ever looked at sheet music, the notes on a page are not the actual notes. They are symbols that indicate the specific sound and the length of the sound that they are referring to.

It definitely takes some time to fully understand this concept. Once you do, I promise you’ll see it everywhere.

What are Objects?

You may be wondering how this ties into object-orientation. An object in programming can be defined as something with specific characteristics and functions that we use to get things done.

In Ruby, everything is an object. For example, a word(s) wrapped in quotations is a string. Strings have built-in functions like .capitalize or .length. Integers are objects as well. Integers also have built-in functions like .to_s, which turns an integer into a string. You can ask for a list of methods for a specific object by typing .methods.

string.methods

If a value has been assigned to a variable, we may not remember what type of object it is. We can find out by typing the name of the variable followed by .class. This is important so that we don’t try to perform a string method like .capitalize on an integer — giving us a TypeError.

x = 90
x.class
=> string

As the gods/goddesses of our program, we can use the concept of abstraction to create our own objects. Of course, this comes with some responsibility. It’s important to maintain logical naming conventions and attributes. Otherwise, other people that collaborate with us on a project will be very confused when you name your ‘dog’ class ‘being’ and give it an attribute of ‘value’. This is not useful and can even confuse the creator if they want to update this code in the future.

Let’s use a physical example. How about popsicles!

From Taylor Herry on UpSplash

When you think of this tasty summer treat, bright colors and delicious flavors naturally come to mind. These are certainly the most important characteristics to assign when creating a ‘class’ of popsicles. It could be translated to code like this:

class Popsicleattr_accessor :color, :flavordef initialize(color, flavor)
@color = color
@flavor = flavor
end
#some more code with functionsend

In this example, each popsicle should have its own unique color and flavor. We can assign these ‘attributes’ to describe one specific popsicle or ‘instance’.

The initialize method is setting up what needs to happen each time we create a new popsicle instance and it is activated when we use the class method .new. Every time we create a new popsicle, we need to pass it an argument of flavor and color. (Because that’s what makes it a popsicle!)

If you’re wondering what @color = color does, it is stating that color is going to be a variable for each individual popsicle that we instantiate using the initialize method. That variable is going to equal the ‘color’ that we pass in as an argument when creating a new popsicle. It might look something like this:

lemon_popsicle = Popsicle.new(yellow, lemon)

This is an instance of a lemon popsicle. We can create as many as we want. Since all the different popsicles are living in the same class, we can create class methods to perform an action on all the popsicles by referring to the class as ‘self’ in a class method. (We won’t go into what ‘self’ means today, but it’s another great topic that I would like to cover in a future post.)

class Popsicleattr_accessor :color, :flavordef self.melt
puts "All the popsicles are melting!"
end
end

OR we can create an instance method to function only on one specific popsicle.

class Popsicleattr_accessor :color, :flavordef melt
puts "This popsicle is melting!"
end
end

You see, objects give us a lot of freedom. We can create an object, assign it attributes that mimic real-world characteristics, and execute the functions we need. This allows us to interact with our code in a more ‘realistic’ way that makes sense for our human needs. The sky is the limit.

Happy Coding!

--

--