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

265
Vistas
How do I work with specific class instances in Ruby?

I want to make a small application where users can create a list of stores and inventories for the stores. Here's what I have for the store class:

class Store
  attr_accessor :name, :inventory
  
  def initialize(name)
    @name = name
    @inventory = []
    @@all << self
  end

end

It initializes with an empty array, and I want to fill it up with items (as strings). I'm aware you can use, say newStore = Store.new to create a store and then further call on newStore, but I want the user to be able to create as many stores as they want and when they create a second newStore it will override that variable. How do I re-access that previous class instance to edit the inventory array?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You can change it to the following and just create a new array when it wasn't initialized before:

def initialize(name)
  @name = name
  @inventory = []
  @@all ||= []
  @@all << self
end
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