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

186
Vistas
Inheritance of constructors in Ruby

Lets say I create a new class which inherits from Symbol. Symbols are unique and it is very clear that the Symbol class has no Constructor. But which mechanism prevents the creation of a constructor in the child class? Is there some flag which makes all children unique?

class Syb < Symbol
   def initialize
   end
end
p Syb.respond_to? :new
false
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

First of all I also believe that is not a good idea to extend ruby core classes, references

In this case in particular

"For efficiency, many core class methods are coded in C instead of Ruby. And in some cases, such as this Array addition operator, they are implemented in such a way that the class of the return value is hardcoded."

If you take a look of the initialize method in the ruby Symbol class (it really has):

[36] pry(main)> require 'pry-doc'
=> true
[72] pry(main)> cd Symbol
[73] pry(Symbol):1> $ initialize

From: object.c (C Method):
Owner: BasicObject
Visibility: private
Signature: initialize()
Number of lines: 5

static VALUE
rb_obj_dummy0(VALUE _)
{
    return rb_obj_dummy();
}

So we can see that this method allready exists and it is implemented in c, but there is a limitation about overiding ruby core classes methods, explained in this answer

And I also believe that is really important to investigate ruby with, pry pry-doc and pry-gem

over 4 years ago · Santiago Trujillo Denunciar

0

As Sergio stated, my mistake was the mix up of constructor and initializer. Actually it is possible to write a new constructor for the Symbol class.

irb(main):001:1* class Symbol
irb(main):002:2*   def Symbol.new x
irb(main):003:2*     x.to_sym
irb(main):004:1*   end
irb(main):005:0> end
=> :new
irb(main):008:0> a = Symbol.new "xyz"
=> :xyz
irb(main):009:0> a
=> :xyz

Yes I realize that here the symbol is actually created by the .to_sym method. So new is not really a constructor but more like a wrapper for the real constructor.

over 4 years ago · Santiago Trujillo Denunciar

0

Lets say I create a new class which inherits from Symbol. Symbols are unique and it is very clear that the Symbol class has no Constructor.

This statement is trivially true, since Ruby has no constructors.

But which mechanism prevents the creation of a constructor in the child class?

The fact that Ruby has no constructors prevents the creation of a constructor.

Is there some flag which makes all children unique?

No.

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