Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

234
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda