Spogg wrote:So often I read something like “The great thing about Ruby is that everything is an object” without any further explanation or even comment.
In the past I tried hard to explain the concept with a series of tutorials over at FlowstoneGURU. But it is a problem to put it in simplest words. I usually tend to give real life examples, but then the reader has to be able to transfer that image onto actual Ruby programming. And if I explain the classes in Ruby themselves, people are missing a relation to something they know.
Maybe a comparison between old-school programming and object-oriented programming will work better?
Here's the picture: You are a programmer employed to write an application that lists all animals of a zoo, with all their aspects, like order or family. Before oop, you would have done something like that:
animal_names = ["lion", "brown bear", "boar", "wild goat"]
animal_class = ["mammal", "mammal", "mammal", "mammal"]
animal_order = ["carnivore", "omnivore", "omnivore", "herbivore"]
animal_family = ["felidae", "ursidae", "suidae", "bovidae"]
animal_species = ["panthera", "ursus", "sus", "capra"]
You now have 4 fixed arrays, and the user of your application might type in a name, then you go through all arrays until you find a match, store the index of the match and list all other arrays' content under that specific index. For example:
input: lion
lion: mammal, carnivore, felidae, panthera
This works, as long as all arrays have the same structure (everything that belongs to the lion needs to be stored under index 0, etc.). It can get more complicated though. Say, the user input is "mammal". Your program code needs to find 4 mammals with all of their properties now. You probably need to install a buffer to store the findings until everything is found, and some kind of system to identify, which ones you already found. And it becomes very uncomfortable, if the zoo gets a second lion.
OOP can make this a lot more easier. I won't go into inheritance (just a bit) and mixins (not at all). But just the most simple aspect of oop already helps you a lot: Creating an object ("class" is a synonym for object) that combines all its properties!
Code: Select all
class Animal
attr_accessor :species, :family, :order, :class, :isit
end
class Lion < Animal
alias_method :is_carnivore?, :isit
def initialize
self.species = "panthera"
self.family = "felidae"
self.order = "carnivore"
self.class = "mammal"
self.isit = true
end
end
george = Lion.new
george.is_carnivore?
You now have a lion object, that you instantiate with "Lion.new". So, no matter how many lions the zoo will get, you are prepared. Searching also gets a lot easier now, as you just ask all the objects, if they have a specific property. (This example is not complete, it is just a quick showcase)
We successfully switched to object orientation (here the lion). We added everything that defines a lion into one closed group - the object or class. I just carefully touched inheritance, as a lion surely is an animal. So I made a base class of animals, which all other animals will derive from. In this class I've put everything that all animals share. Here they all share the properties themselves (in Ruby known as attributes), but not the content of them. But when I create a Lion instance, it inherits all properties from the base class Animal, and can fill them in with its own specific data.
I'm pretty sure, this attempt will again not be understood - but hey, I tried
