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

237
Visualizações
Listing parts of a class

I am trying to make a code with a cat, where you are able to buy certain toys and use them. However, whenever I try to list the toys in the "shop," it gives me an error every time. I'm not sure what I'm doing wrong, but it is definitely somewhere in how I'm executing the command.

class Toy
  attr_reader :name
  
  def initialize(name)
    @name = name
  end

  def shop
    puts "#{@Toy}"
  end
end

toy.shop

I am not sure how to properly run this command, but I'm fairly certain the problem is in the "toy.shop" line.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

A Working Rewrite and Simplification

There are a number of issues with your current code. Since there's always more than one way to do almost anything in Ruby, you can do this in other ways, too. However, here are some key issues:

  • You're not really leveraging #initialize.
  • You don't instantiate a new Toy object with Toy#new.
  • You don't assign Toy#new to a local toy variable.
  • Because toy is not defined, it doesn't have access to any of Toy's instance methods like #shop.
  • The non-existent toy variable is the receiver for the #shop method, so it will raise NameError if toy is undefined, or NoMethodError if toy is defined but not an instance of the Toy class or another class that can respond to the #shop method.

Based on your limited example, I'd rewrite and simplify the code as follows:

class Toy 
  attr_accessor :name
  
  def shop
    p "You bought a #{@name}."                                                  
  end
end

toy = Toy.new
toy.name = "mouse"
toy.shop
#=> "You bought a mouse."

Why Kernel#p Instead of Kernel#puts

Note that Kernel#p will not only print your output to STDOUT, but will also return a value, which is extremely useful in a REPL or when debugging code. Kernel#puts, on the other hand, always returns nil so I generally prefer #p unless the output from #inspect is undesirable for some reason. Your mileage in that regard may vary.

See Also

  • The defined? keyword.
  • Variable assignment.
  • Ruby syntax.
  • Ruby exceptions.
over 4 years ago · Santiago Trujillo Relatório

0

First of all, this is a class, you should initialize and then call the method like this: Toy.new('some_toy').shop

And there is an error in your shop method, @Toy doesn't exists. It should be:

  def shop
    puts "#{@name}"
  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