Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

243
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!