Reputation: 89
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.
Upvotes: 2
Views: 229
Reputation: 2073
Well, this looks like a homework question, which I'm not going to write for you. However I'll give you some pointers.
You create/ open up a class like this.
class Foo
end
When you open up a class like this you can set it up to accept arguments immediately, like so:
class Foo
attr_accessor :bar, :bar_counter
def initialize(arg_1)
@bar = arg_1
@bar_counter = 0
end
# And add methods with any name like so.
def increase_bar
@bar_counter += 1
end
def change_bar(arg)
@bar = arg
end
end
This will explain the differences between attr_accessor, attr_reader, attr_writer https://stackoverflow.com/a/4371458/2167965
People have various opinions on Codecademy, but in my opinion it's perfect for teaching basic syntax like this. There's also Ruby Koans, and Ruby Test First.
My recommendations would be to start with codecademy to learn the syntax, and move to Test First to flesh out those concepts.
Upvotes: 2
Reputation: 27207
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)
Upvotes: 4
Reputation: 168101
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
Upvotes: 3