Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

232
Visualizações
Creating anonymous classes in ruby

According to the "well ground rubyist" in the example c "contains" or "points at" an anonymous class. Am I right, that C is not an anonymous class? That's really a little bit confusing.

    >> c = Class.new
    => #<Class:0x0000000006c4bd40>
    >> o1 = c.new
    => #<#<Class:0x0000000006c4bd40>:0x0000000006b81ec8>
    >> o1.class
    => #<Class:0x0000000006c4bd40>
    >>
    >> C = Class.new
    => C
    >> o2 = C.new
    => #<C:0x0000000006494480>
    >> o2.class
    => C
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

  • Class.new creates a new anonymous (unnamed) class

  • c = Class.new creates a new anonymous class and assigns it to the local variable c

  • C = Class.new creates a new anonymous class and assigns it to the constant C

Although both assignments look almost identical, there's a subtle but important difference: assigning an anonymous class to a constant sets the name of that class to the name of the constant:

c = Class.new
c.name #=> nil

C = Class.new
C.name #=> "C"

This is also mentioned in the docs for Class.new:

You can give a class a name by assigning the class object to a constant.

At this point, the class is no longer anonymous. It now has a name and we can refer to it using that name in the form of the constant C.

over 4 years ago · Santiago Trujillo Relatório

0

The concept of an "anonymous" or "named" class doesn't really make sense in Ruby. A class is just an object like any other object. We don't have "anonymous" or "named" integers or strings either. A class is just an object like an integer or a string: it can be returned from a method, it can be assigned to a local variable, instance variable, class variable, global variable, or constant.

This is different from programming languages like Java with a nominative type system, where a type having or not having a name makes a big difference.

This is really important to understand: a class is just an object that happens to be an instance of the Class class. Nothing special. It is just an object like an integer or a string or an array.

What classes (actually modules) do have, is a method named Module#name, but again, there is nothing special about that method. You can add a #name method to your own classes as well.

The only thing that is special is that when a class (actually a module) is assigned (whether using the assignment operator =, or reflectively using the Module#const_set method) the first time to a constant, the path to that constant becomes the return value of Module#name:

a = Module.new
a.name #=> nil

b = a
b.name #=> nil

module A; module B; module C; end end end

A::B::C.const_set(:D, b)

a.name #=> 'A::B::C::D'

A::B::C::D = nil
A::B::C.send(:remove_const, :D)
A::B::C = nil
A::B.send(:remove_const, :C)
A::B = nil
A.send(:remove_const, :B)
A = nil
Object.send(:remove_const, :A)

a.name #=> 'A::B::C::D'

So, as you can see, the distinction between "anonymous" and "named" is practically non-existent, and even if you could construct a meaningful definition, then the distinction is still irrelevant.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda