My First Crack at OOP in Ruby
Published: 2020-01-26 00:28:52

Object Oriented Programming (or OOP for short) is a type of computer programming centered around -you guessed it- Objects. But what is an Object? And what does it mean for a programming language to be centered around the concept of Objects?

Let's start by digging in to that first question: what is an Object?
Unless you are a monk or a nun who is living in solitude and breaking your vows to access this blog (I'm honored) you're likely familiar with the team of superheroes known as "The Avengers". One of the most recognizable members of this super-team is the hero known as "Captain America". For the sake of this explanation, Captain America is going to stand in as our Object.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/0pp0k32z9q9iqhprjnys.jpeg)

As if he hasn't been objectified enough over the last 80 years (sorry Cap).

In programming, an Object is an instance of a Class. Don't panic, we don't need to dive too deep into Class or instance right now. All you need to know is that a Class is a blueprint for creating instances of Objects. For example, we could say that our Object, Captain America, is an instance of the Class "Superhero". He is a Superhero Object. Other such instances of the class "Superhero" could be Spider-man, Wonder Woman, or Batman. At some dark, Avenger-less point in time, there was no such Object as Captain America, but then Joe Simon and Jack Kirby dreamed up a new instance of the Class "Superhero", and our beloved Captain Object was sketched into existence.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/bq0fx7at24kucxexlgdg.png)

An Object has two defining features: attributes and behaviors. All human-like "Superhero" instances universally share a few basic human attributes. What are some of Captain America's? Let's see; hair: blonde, eyes: blue, shield: star-spangled. We can assign these descriptive traits to the Captain. But even the coolest attributes are not enough to fight crime on their own. Blonde hair doesn't punch, a boomerang shield isn't going to throw itself! To make use of these attributes our Object needs some behaviors.

Without behaviors (which manifest in code as methods or functions) our hero Object would just be a lifeless robot. He needs to be able to walk, run, jump, and kick! Maybe even shout a catchphrase?

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/5aomp9onuwcbpa90bbnc.png)

Defining methods and functions for our Objects gives them the framework they need to interact with the world (or program, in our case) around them. A superhero can't just jump straight to fighting crime without first going through a training montage!

And with that step complete, we have now created a very (very) basic instance of an Object (in Ruby, which is an Object Oriented Programming language). The beauty of Objects is that they are self contained and independent of other Objects. They store a lot of data under the hood (Captain America has almost a century of knowledge stored under that helmet of his) and they provide a framework that can be reused or repurposed (maybe we could use Cap's "Superhero" class to make some more Avengers?).

And last but not least, they make debugging easier. Let's say we used this Superhero class to build out an Avengers (software) program, but one of our creations (maybe the Hulk?) doesn't quite fit in with the rest of the team and starts causing trouble aka bugs. To get our program back up to full crime fighting capacity, we could simply replace that Object with another hero Object that's a better fit (She-Hulk, perhaps?) without having to tear down the whole team (program) and rebuild it from scratch.

I hope this example is easy to understand and helps demystify OOP just a little bit for you. Until next time, true believer, keep programming and fight for "Truth, Tolerance, and Justice" - Clark Kent.