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

266
Visualizações
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 Respostas
Responde à pergunta

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 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