Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing a class from example of use

Tags:

class

ruby

I need to make the following code functional by building a "Car" class. I feel I must be overlooking something simple. any help would be appreciated. The # indicates the expected output

# Make the following code functional by building a Car class
c = Car.new("blue")
puts c.color # blue
puts c.repaint_count # 0
c.paint("red")
c.paint("green")
puts c.repaint_count # 2
puts c.color # green

here is what I have done:

class Car
  @@repaint_count = 0
  def initialize(color)
    @color = color
  end

  def self.paint(color)
    @color = color
    @@repaint_color += 1
  end

  def self.color
    @color
  end
end

I guess I am being thrown by the c.color / c.paint: should I be defining these methods and setting them equal to class or something else ? I think I am missing something about classes and inheritance.

like image 434
imagineux Avatar asked Mar 09 '26 14:03

imagineux


2 Answers

I guess I am being thrown by the c.color / c.paint: should I be defining these methods and setting them equal to class or something else ? I think I am missing something about classes and inheritance.

I think in fact you are over-complicating it by worrying about these things at this stage. The question is not about inheritance. Although in some ways it is poorly specified in that it is possible to mis-interpret the question text and assign some properties to the class other than the instance.

So first things, you have got that the question expects you to implement a Car class, and that there is internal state to track for the current color and the number of times it has changed. You have partly mis-understood the repaint count and made it a class variable. It needs to be an instance variable - it is intended to be the number of times a specific car has been re-painted, not the number of times any car has been re-painted. Although the example numbers would be the same, the difference is that the question asks for c.repaint_count not Car.repaint_count, and c is an instance of Car, hence you want to store the count as an instance variable - set it to 0 in the constructor.

Similar confusion in your accessor code. Ruby's use of self is a little confusing - it changes meaning on context in the code. If you changed your def self.paint to just def paint and similarly for color then with the change from last paragraph, you are pretty much done.

One last thing, you need to implement repaint_count accessor similar to how you have done with color (and again, without the self. which would make it a class method)

like image 113
Neil Slater Avatar answered Mar 12 '26 05:03

Neil Slater


You seem to be confusing classes and instances. c is an instance of Car, and is not the class Car itself. Unless you want to count the total repaint_count throughout the Car class, you should not use a class variable @@repaint_count, but should use an instance variable. Your paint method is a class method, and is not well defined. In addition the definition body looks like you randomly put something.

class Car
  attr_reader :color, :repaint_count
  def initialize color
    @color = color
    @repaint_count = 0
  end
  def paint color
    @color = color
    @repaint_count += 1
  end
end
like image 26
sawa Avatar answered Mar 12 '26 03:03

sawa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!