dimanche 28 juin 2015

Understanding classes in ruby

I have been doing a lot of reading around Ruby classes and singleton classes. I came across a really interesting article that jolted a question I haven't been able to find a clear answer to yet.

This topic is pretty confusing so hear it goes

If you have a class Dog

class Dog
  def talk
    "bark"
  end
end

This class is the blueprint of any instances of Dog. Ruby stores this blueprint as an object when loading the file/class into memory - e.g. when requiring a file. This object is an instance of the Class class.

Dog.class => Class

So all classes are actually stored as blueprint objects of type Class. The concept of a class is just a means to refer to it with a name, but when dealing with it in Ruby you are actually interacting with objects.

Is this assumption correct?

Aucun commentaire:

Enregistrer un commentaire