Object Oriented Ruby

I wanted to find out how to create classes and objects in ruby so I started with the ruby documentation and google. I was interested in finding out exactly what objects mean to ruby and then compare it to how other languages like PHP, Java, Python deal with objects, by this I am referring to the actual language, what is an object in the language and what isn’t. How object oriented can a programming language really get.

What is the fuss about objects or oop?

Well OOP I found is not a programming language, doesn’t belong to any particular language neither. Instead it is a problem solving concept, a way of visualising any problem and breaking it down into logical and more natural grouping.
OOP is to a programmer what a canvas is to an artist. First thing in planning any system is thinking of what objects will be born and how they will interact with each other. I have touched on this in previous post, but now I wanted to look more at what makes a language good for OOP.

What is the opposite of OOP

Well it sounds like the opposite of OOP is “hell” it is also better known as “procedural programming”, in this you have a collection of functions that achieve a goal, it is a nightmare to maintain as immediate grouping is not apparent, re-useable code is not easy.

Consistency…Does it matter?

Most programming languages claim to be object oriented, but I was interested in finding out what makes a language good as a facilitator of OOP, when I came across ruby it made a bold claim “Everything in ruby is an object”, at first I did not take this seriously, because I have coded in PHP and I have been able to create classes and objects no problems there, but as I played with ruby it made sense that ruby should brag about everything in ruby being an object.

We know that OOP is not a programming language, it is a problem solving philosophy, a way of visualising problems and creating solutions, imagine if the language you are coding with was it self made up of objects, this means that when it comes to solving your problems, you have a language that is already 100% structure in the philosophy you believe in, no need for nasty hacks or misunderstanding in communications between your thoughts and your language.

Imagine situation, where you have a language that is partly object oriented, that means that when it comes to using some of its core functions there is break in consistency, am I using this function in an object oriented fashion or not? this is the case in languages that aren’t completely object oriented like my old beloved PHP, I didn’t really know what I was missing until I started learning ruby.

Understandably Ruby started off right from the beginning as an object oriented language, and PHP is only recently adopting this, but far from making a claim that everything is an object, therefore loosing out on the pure passion and wholeness I am now getting from Ruby.

Everything is an object in Ruby

When Ruby said that everything is an object, it wasn’t kidding neither, Ruby is a genuine object-oriented language. Everything you manipulate is an object, and the results of those manipulations are themselves objects. imagine typing the following:

   1.class #returns the class object for 1
   'hello kingsley'.class #returns the class object for a string

So the final evidence of a completely object oriented language, will be those languages where even the primitives are themselves objects.

In many languages, numbers and other primitive types are not objects. Ruby follows the influence of the Smalltalk language by giving methods and instance variables to all of its types. This eases one’s use of Ruby, since rules applying to objects apply to all of Ruby.