2

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.

imagineux
  • 89
  • 5
  • 4
    Can you show us what you have tried? We can't help you fix your code without seeing what you currently have. – Justin Wood Jan 03 '14 at 16:26
  • 6
    That is not called reverse engineering. Reverse engineering is to figure out how something already made works, and to copy it. What you are trying to do is more like test driven development, except that you are not trying to develop, and are just asking someone to do it for you. – sawa Jan 03 '14 at 16:27
  • 2
    @sawa: I've fixed title for OP, as it is a minor side issue. To OP: What is needed to get help are details of what has been tried so far - ideally enough code to show where you are stuck - then the answers can be short and to the point. It would be trivial for an experienced developer to just hand you the completed implementation, but that isn't really helping you. – Neil Slater Jan 03 '14 at 16:32
  • here is what I have:' class Car ' – imagineux Jan 03 '14 at 16:39
  • @imagineux Did something happen when you pasted the code in the comment, or is that really all you have so far? – Paul Richter Jan 03 '14 at 16:42
  • 1
    @imagibeux: If you are stuck with understanding the question text before writing a line of code, are you able to explain where the confusion arises? It is probably more useful to fix that for you, and equip you for the next question. Converting requirements to code is a core developer skill, and it is worth spending time figuring out and fixing this. – Neil Slater Jan 03 '14 at 16:44
  • 2
    Actually I think this is answerable now. – Neil Slater Jan 03 '14 at 16:49

3 Answers3

4

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)

Neil Slater
  • 26,512
  • 6
  • 76
  • 94
3

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
sawa
  • 165,429
  • 45
  • 277
  • 381
2

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.

Community
  • 1
  • 1
MCB
  • 2,021
  • 1
  • 18
  • 32
  • 2
    `attr_accessor` is redundant if you are not using the setter method. – sawa Jan 03 '14 at 16:46
  • @sawa he's asking about opening up classes and creating methods on day one the goal is just to create some logic that works. It took me a few days or a week of understanding what a class was before I even looked at what a getter or setter is. – MCB Jan 03 '14 at 16:54
  • If that is the case, you should not be using `attr_accessor`. It should be too advanced without knowing what a getter or a setter is. – sawa Jan 03 '14 at 16:55
  • @sawa I think not getting an error at this stage is probably the most important. But I'll but a link to explain what it's doing. – MCB Jan 03 '14 at 17:05