So I have many child classes that will inherit from a parent class. I’ve been playing around with instance variables @ and class variables @@ and I have yet to be able to achieve with them what I want. What I want actually works with the code below but it doesn’t seem DRY at all. Any suggestions on how I can refactor this?
class Planet
def has_color?(color)
self.color == color
end
def has_position?(position)
self.position == position
end
end
class Mars < Planet
def color
"red"
end
def position
4
end
end
class Earth < Planet
def color
"blue"
end
def position
3
end
end
What I hope to achieve
>> Mars.has_color?("red")
true
>> Earth.position
3
is this useful for you ? I have tried , it works in Ruby1.9.2