DEV Community

Discussion on: Object Oriented Ruby

Collapse
 
tadman profile image
Scott Tadman • Edited

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

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

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.