T
Thufir
<http://www.javaworld.com/javaworld/jw-06-2001/jw-0608-java101.html> has
a discussion of composition versus inheritance which I'm trying to apply
to ruby. For discussions sake, I'll use the classes discussed in the
above linke: Car, Vehicle and Engine. Car inherits from Vehicle, of
course.
Let's say that Engine is something like:
class Engine
attr_accessor :type
def initialize ()
@type = "generic engine"
end
def vroom ()
puts "vroooooom"
end
end
Vehicle would be something like:
class Vehicle
attr_accessor :engine
def initialize ()
@engine = Engine.new
end
end
class Car < Vehicle
def initialize ()
super()
end
end
then from IRB for instance:
vw_bug = Car.new
then can you make the vw_bug go "vroooooom" and so forth? What would be
the usual way to do this in ruby? When a car is instantiated, should an
engine object get passed to the initializer method?
thanks,
Thufir
a discussion of composition versus inheritance which I'm trying to apply
to ruby. For discussions sake, I'll use the classes discussed in the
above linke: Car, Vehicle and Engine. Car inherits from Vehicle, of
course.
Let's say that Engine is something like:
class Engine
attr_accessor :type
def initialize ()
@type = "generic engine"
end
def vroom ()
puts "vroooooom"
end
end
Vehicle would be something like:
class Vehicle
attr_accessor :engine
def initialize ()
@engine = Engine.new
end
end
class Car < Vehicle
def initialize ()
super()
end
end
then from IRB for instance:
vw_bug = Car.new
then can you make the vw_bug go "vroooooom" and so forth? What would be
the usual way to do this in ruby? When a car is instantiated, should an
engine object get passed to the initializer method?
thanks,
Thufir