A class is an instance?

Asked

Viewed 96 times

4

Considering the example, one can say that p1 is an instance of Pessoa, which is the class:

class Pessoa
end

p1 = Pessoa.new()
# => #<Pessoa:0x00000001268ee528>

However, it is not often said that Pessoa, the class in the previous example is also an instance. Taking Ruby again, see which class it is from Pessoa:

Pessoa.class
# => Class

I can tell you that Pessoa is an instance of Class? Although I used Ruby to exemplify, language-independent, a class is always an instance of something that defines what classes are more generic?

2 answers

4


Generally speaking, it is not. What is the difference between a class and an object?. Object in the case is the instance.

Just to make something clear before:

p1 is a variable. It has a value, so it’s an object that has there, it’s the instance of something. It doesn’t have to be a class. And in fact in almost every language that defines a class it models an object that will be stored by reference, so what it has in p1 is an instance of a pointer to an instance of another object, in the case modeled by the class Pessoa. This is the correct definition and should be understood when one wants to learn right. Informally we say that the object is in p1, but he’s not.

Understood this I think the question is about Ruby, the demonstrated in the code does not exist in other languages. Including much of what it shows, which would explain what is set, is something not only of Ruby, but of this implementation of the language.

Ruby uses a concept of metaclasses, so a class can be an instance of a metaclass (note the difference that it is not a normal class). Instance of a class is an object, instance of a metaclass can be a class. Although the name is "instance" in both cases are different things. Although it can be said that a class in Ruby does not cease to be an object by the philosophy of the language of objects being first-class citizens.

Again, this is something from Ruby and one language or another. Not applicable to most object-oriented languages (not to mention those that even use classes).

So to leave the generic answer, languages that do not treat classes as objects and have no metaclasses, classes are not instances of anything.

In Ruby the correct use of terminology is to say that Pessoa is an instance of Class, is in the documentation. I don’t know how they use it informally.

My opinion is that Ruby is closer to Javascript, IE, has classes, but at the bottom are prototypes, so you have objects that are used as groundwork for other things, including other classes, which are still objects. They are not exactly models because it occurs in real class.

It is important to separate the correct from what we use informally in day-to-day life. You can understand the informal, but it can explain wrong to the unsuspecting.

0

Class is the recipe and the instant is the cake.

Below the representation of a class ( only the structure ) containing the name attribute ( also part of the structure ) So far we have only the structure and we have no instance/creation of its use in memory.

class Pessoa{
    nome;
}

Below we create two instances of the structure/class above.

Pessoa p1 = new Pessoa(); // colocando uma instancia da classe pessoa na memória
Pessoa p2 = new Pessoa(); // colocando OUTRA instancia da classe pessoa na 

p1.nome = "nome A";
p2.nome = "nome B";


We see then that P1 and P2 have the same structure the same attributes. but they are two different instances of the class person. until we add different values for the instances.

  • 2

    He is talking about the class being an instance and not an object, which is always.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.