DEV Community 👩‍💻👨‍💻

Brittany
Brittany

Posted on

Object Oriented Ruby

The introduction of object orientation in Ruby seems to be well-timed and intuitive within this course. The ways that we learned to build methods before are now making sense in the context of a class. Before learning about object orientation, I kept wondering where all these methods were going to be used, how they tied together, and what was their ultimate purpose. Now that object orientation has become the name of the game, it’s clear that we use these methods to bring objects to life.

One of the most recent ‘Aha!” moments that I’ve had was related to the reader and writer methods that are involved in building a class. When OO was first introduced, and the process of defining the #name= and #name methods was being taught, I felt like I was mechanically typing the same thing over and over without comprehending what was happening. Why did we need two different methods to represent the same variable? And what does that = sign do? I didn’t get it.

Then, somewhere along the way, I realized that when we call name= (or any variable=) in our program, we are setting that variable equal to a value for a particular instance of a class. We are writing that variable’s value. We also need an additional method to recognize that value and allow it to be referenced and operated on with all the capabilities that the class has to offer, hence the necessity for the reader method that includes the @variable, the instance variable. The instance variable is recognized throughout the scope of the class.

I finally wrapped my mind around this concept when along came attr_accessor to replace these two methods! Well, not always, but in many cases. Writing out the reader and writer methods is the foundation, and using attr_accessor is the next level of abstraction that streamlines our code and makes our lives easier. I get it!

Thank you for reading : )

Top comments (6)

Collapse
tadman profile image
Scott Tadman • Edited on

Ruby's object-oriented approach is a bit unusual compared to others because everything is an object which means the usual distinction between objects and non-objects doesn't exist.

A lot of classic computer-science type object-oriented training tries to describe this separation between fundamental types (primitives) and classes and instances. In Ruby it's all objects, so that's kind of irrelevant.

Ruby's value= mutator method is what's called in other languages an "lvalue", that is it can be used on the "left side" of the = assignment operator. All that means it that method gets called when you do self.value = x. This is super handy because it avoids all the ugly getValue() vs. setValue(x) type calls necessary in other languages.

Collapse
brittanygrebnova profile image
Brittany Author

Scott, thank you! You touched on something that I still don't understand, which is why it's called object-oriented Ruby. My guess was that it differed by calling a class and writing methods inside it, as opposed to the procedural Ruby we initially learned (just defining methods without classes). Is this right?

Collapse
tadman profile image
Scott Tadman • Edited on

When it comes to teaching most languages like JavaScript, Python or C++ it's usually best to start with the procedural approach and then build up to object-oriented concerns once you've got a handle on the fundamentals.

Ruby is unusual in that since everything, literally everything is an object, you sort of have to start with that and work towards the procedural style after you understand what that means.

In practice this is actually a powerful learning tool since when you're using an interactive shell like irb you can explore what's possible with the values you're manipulating very easily.

If you want to know what a number is:

1.class
# => Integer

That means you can consult the Integer documentation where all the things you can do with those types of objects are described in detail. This isn't true in other languages where things like + and - are not defined the same way.

The second thing to embrace is that everything in Ruby involving objects is a method call, even something as simple as x = 1 + 2.

That's equivalent to:

x = 1.send(:+, 2)

Where :+ is the name of the method here expressed as a symbol, or in other words, Integer#+ in Ruby's instance method notation.

What this means is if you want to do anything in Ruby there's a method involved, and if there's a method involved it's probably documented. This is makes finding out how to do things properly really easy as you know exactly where to look for documentation.

Ruby's Integer#+ is completely separate from String#+ and Array#+ meaning you don't have the confusion in other languages like JavaScript where the same + operator applies to a whole host of things.

So Ruby's a bit of an odd language when compared to others because of its approach, but it's actually extremely beginner friendly because of it.

Collapse
lysofdev profile image
Esteban Hernández

Don't forget attr_reader and attr_writer.

Collapse
brittanygrebnova profile image
Brittany Author

Aleksei, thank you for the clarification! I left this post here with the hope of getting feedback like yours, so I appreciate you taking the time to read and reply. I wrote this post in March as a requirement for the Flatiron course in which I'm enrolled, and will be uploading all my other posts here too. Please reply freely!

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.