I feel slightly anxious writing about such a complex subject as object oriented programming having only been studying ruby for a short time but here goes.

In ruby everything is an object. Programs are made up of many objects and these objects can communicate and interact with one another.

An object has ‘states’ and ‘behaviours’. In a real world example lets say our object was a person. Our person might have a name, gender and an age (all states) and be able to speak and meet other persons (behaviours). What our person (object) is called, their age and gender is up to us and what our persons can do is also up to us because we create the blueprint for persons when we write our Person class. It might look something like this:

 1 class Person
 2   @@total_number_of_people = 0
 3 
 4   attr_accessor :name, :age, :gender
 5 
 6   # THIS IS WHERE THE STATES ARE CREATED AND STORED IN INSTANCE VARIABLES (@name, @age, @gender)
 7   # THE @@total_number_of_people IS A CLASS VARIABLE (more on these in a bit)
 8   # THEY ARE SET BY THE ARGS WE PASS IN DURING INSTANTIATION (more on that in a bit)
 9   def initialize(name, age, gender)
10     @name = name
11     @age = age
12     @gender = gender
13     @@total_number_of_people += 1
14   end
15   
16   # THIS IS AN INSTANCE METHOD
17   def meets(person2)
18     if self.age > 18 && person2.age > 18 
19       Person.new('baby', 0, ['male','female'].sample)
20     else
21       puts "Don't do that"
22     end
23   end
24 
25   # ANOTHER INSTANCE METHOD
26   def speak
27     if self.age > 2
28       puts "I can talk!"
29     else
30       puts "wha wha wha wha!"
31     end
32   end
33 
34   # THIS IS A CLASS METHOD (I'VE NOT MENTIONED THESE AS YET)
35   def self.total_persons
36     @@total_number_of_people
37   end
38 
39 end

That is an example of how we create a class that can be instantiated to create as many objects as we like. To actually create a new person (an object or instance of the Person class) we do this:

jason = Person.new('Jason', 38, 'male')
vic = Person.new('Victoria', 37, 'female')

So now I have created myself and my girlfiend Victoria and stored us in variables jason and vic. Just like real people jason and vic can interact and magic things can happen when people interact. If you look back on the Person class we wrote we can see just what behaviour me and Vic can get up to. We can also check the worlds population using the class method we created that in turn returns the class variable we created.

CLASS VARIABLES AND CLASS METHODS

Already our godlike Person class can do some amazing stuff like create people and allow them to do things but our Person class can also have a sense of self too. What I mean is it can keep track of things that are not specific to each object it creates like how many people it has created in total. To do that we need a class variable and a class method to call to get the information (again state and behaviour).

To define a class variable we use two @’s like we did in our Person class above (@@total_number_of_people). To define a class method with simple prepend self. to the beginning of our method name like we did for the total_persons method.

So now lets check the world’s current population:

Person.total_persons
# => 2

How easy was that?

OK back to our objects - and by the way our Person class itself is an object and it was created from the class Class (Let’s move on). We now have a world population of 2 and those 2 people are objects (jason and vic). jason and vic can interact because we made it so in our class with the instance method meets. This method is almost as amazing as the Person class itself as it too creates new life so long as certain conditions are met. We call it like this:

baby1 = jason.meets(vic)

This is simple a variable called baby1 which is assigned whatever the meets method returns and in this case it’s a brand new baby (a new object).

WHY DO WE NEED OBJECTS?

As the worlds population grows and we start to evolve beyond just breeding and speaking our program becomes more complex. For instance not every one speaks English and even for those that do there are many different regional accents and add to that the fact that each persons vocabulary is likely to be different depending on their age and you soon realise that the human race is incredibly complex. Now imagine if we didn’t have our wonderful Person class where we centralize and group many of the elements that make up what a person. Without the Perosn class creating new people would create a huge amount of repetitive code and if we wanted the human race to evolve a third rear-facing ear we would need to change every single person in the world one by one.

That’s about as much as I feel comfortable in talking about regarding OO at the moment.

A bit more on Object Oriented

Alan Kay likened it to a cellular metaphor. Each object (not class) is a cell. It can do some stuff internally, but only very simple things. In order to collaborate with other cells, it sends out a message and cells that are interested in that message perform their actions when they receive it.

A short read of Alan Kays definition of Object Oriented.