• Home
  • Interactive Resume
  • Blog
    • Coding
    • Health & Fitness
    • Success & Lifestyle
  • Contact
  • About

BAOSS

  • Home
  • Interactive Resume
  • Blog
    • Coding
    • Health & Fitness
    • Success & Lifestyle
  • Contact
  • About

Learning Ruby: The Object Model

Home Coding Learning Ruby: The Object Model
Learning Ruby: The Object Model

Learning Ruby: The Object Model

Jan 18, 2017 | Posted by Bao Le | Coding |

Object-Oriented Programming(OOP) is the essence of Ruby, and as such, it’s hard to really elaborate what an object is. At first, it was hard for me to grasp what an object was, let alone the concept of OOP, but with more repetition, exposure and practice, it got easier and easier to understand what OOP was. Now the challenge is to teach what I’ve learned. In this article, it is my goal to explain what OOP is to an absolute Ruby beginner, to explain it in a short and concise way that I could have understand it a couple weeks ago when I was struggling with the concepts; and through teaching it to you, I hope to reinforce my knowledge on the subject as well. The best way I learn, and the best way I know how to teach someone is through examples and visuals.

First of all, though the concepts here will be written for Ruby, through my experiences with practicing JavaScript the last couple days, the concept of OOP also applies to JavaScript, and I would assume to very many other languages.

What Are Objects?

Throughout tutorials and readings on Ruby, you’ll hear that everything in Ruby is an object. I don’t mean to be repetitive and reiterate that concept but it is. Everything in Ruby, just like in real life, is an object. Now you’re probably like, “Yea Bao, but what exactly does that mean? What, in laymen’s terms, is an object?” In terminology that I would probably use to try and explain this concept to someone in person, an object is a singular entity with qualities or attributes that are unique to it (also to note, these qualities or attributes are Objects themselves. Talk about object-ception).

For example, humans are objects with attributes. Each object, when created, has a unique set of attributes. In the real world, when a baby is born, they have attributes such as a name, eye color, hair color, height, weight, number of fingers, etc. Same goes for a code example:

class Person
  attr_reader :name, :eye_color, :hair_color

  def initialize(name, eye_color, hair_color)
    @name = name
    @eye_color = eye_color
    @hair_color = hair_color
  end

  def greet
    print "Hello!"
  end

  def run
    # Some code for running
  end

  def work
    # Some code for working
  end

end

In the code above, a class was created called Person. This person class, when created immediately receives a name, eye color, hair color, and also has the ability to greet, run and work. And each time you create a new “Person,” you’ve created a new Object that has a name, eye color, etc. So let’s create a new person, we’ll call him Bob.

person1 = Person.new("Bob", "Brown", "Black")

When we type that line into the console, the object for Bob, is stored in person1. And when we check in the console/terminal for what person1 is, we see that person1 is named “Bob”, has “Brown” eyes and “Black” hair.

[2] pry(main)> person1 = Person.new("Bob", "Brown", "Black")

=> #<Person:0x007fe2aba7b170
 @eye_color="Brown",
 @hair_color="Black",
 @name="Bob">

Let’s create a new person. We’ll call her person2 for simplicity.

[3] pry(main)> person2 = Person.new("Susan", "Blue", "Brown")

=> #<Person:0x007fe2ac0ecc98
 @eye_color="Blue",
 @hair_color="Brown",
 @name="Susan">

What we have now is a new Object. That object is stored as the variable person2 and has the name “Susan.” She has “Blue” eyes and “Brown” hair. Take a look at the graphic below:

Each time you create a new instance from the Person class, you created a new object. We have two primary objects above, labeled person1 and person2 like before. Each person/object in the diagram has attributes that are unique to it that we initialized when we created that instance of the Person. In order to access these attributes, you simply type: person1.name to access “Bob” or person2.eye_color to see what “Susan’s” eye color is (blue). I hope that this diagram helps you understand what object is, this is was the point in which it clicked for me.

Now that you have a better idea of what an object is, I just wanted to quickly re-address the concept I mentioned previously of “object-ception”, (self-named, it’s not a technical term). In the case of person2, not only is person2 an object from the Person class, but person2’s name, “Susan,” is an object from the Ruby default String class. If this is a bit confusing for you, no worries, you can ignore this paragraph and revisit it later after a bit more practice and exposure to objects.

In the next post we’ll take a look at examples of how Ruby, as an OOP language, centers around these objects.

0
Share

About Bao Le

Web Developer at Dropbox. Reach out, I'd love to connect!

Latest Posts

From Bootcamp to Dropbox: Part 2 – Success in the Program

From Bootcamp to Dropbox: Part 2 – Success in the Program

November 27, 2017

Before reading Part 2, make sure you’ve read Part 1!...

From Bootcamp to Dropbox: Part 1 – Application Process

From Bootcamp to Dropbox: Part 1 – Application Process

October 9, 2017

The Decision The decision to go into programming, without...

Signed with Dropbox!

Signed with Dropbox!

September 8, 2017

After a grueling 3 months or so, my job search has come to...

How I Overcome Rejection and Stay Motivated

How I Overcome Rejection and Stay Motivated

July 17, 2017

Having just had my first real rejection from a company I...

How I Overcome Imposter Syndrome Every Single Day

How I Overcome Imposter Syndrome Every Single Day

May 22, 2017

Before I really go into how I personally overcome imposter...

Future Googler

Future Googler

May 2, 2017

I’m finally “finished” with my interactive...

Introducing the Graduate Job Seeker Program

Introducing the Graduate Job Seeker Program

April 26, 2017

I almost forgot to blog today haha. Please expect not as...

Progress on Progress

Progress on Progress

April 25, 2017

The end of Week X Day X titles are here! And with it marks...

Week 13 Day 5 – Google Co-Working Meetup

Week 13 Day 5 – Google Co-Working Meetup

April 24, 2017

The Google meetup was absolutely dope. It was an incredible...

Week 13 Day 4 – Catching Up

Week 13 Day 4 – Catching Up

April 21, 2017

I think I have a pretty solid game plan of how to attack my...

Contact Us

We're currently offline. Send us an email and we'll get back to you, asap.

Send Message

Let’s Connect!

© 2025 · imbaoss.com

  • Facebook
  • Twitter
  • Instagram
  • LinkedIn
  • GitHub
Prev Next