DEV Community

Discussion on: Object Oriented Ruby

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.