58.2k views
3 votes
In Ruby, define a module and base class that these two classes can include to describe common data and functionality for the classes: class Car < YourBaseClass include YourModule end class Truck < YourBaseClass include YourModule end

User MandyW
by
5.1k points

2 Answers

1 vote

Answer:

Check the explanation

Step-by-step explanation:

class Car

def to_s

"Car"

end

def speed

"Top speed 100"

end

end

class Truck < Car

def speed # Override

"Top speed 200"

end

end

car = Car.new

fast_car = Truck.new

puts "#{car}1 #{car.speed}" # => Car1 Top speed 100

puts "#{fast_car}2 #{fast_car.speed}" # => Car2 Top speed 200

//Module Of Car:------

Vehicle = Module.new do

def wheels

100

end

end

class Car

include Vehicle

end

c = Car.new

p c.wheels

User Heah
by
4.2k points
5 votes

Answer:

c = Car.new

p c.wheels

Step-by-step explanation:

class Car

def to_s

"Car"

end

def speed

"Top speed 100"

end

end

class Truck < Car

def speed # Override

"Top speed 200"

end

end

car = Car.new

fast_car = Truck.new

puts "#{car}1 #{car.speed}" # => Car1 Top speed 100

puts "#{fast_car}2 #{fast_car.speed}" # => Car2 Top speed 200

//Module Of Car:------

Vehicle = Module.new do

def wheels

100

end

end

class Car

include Vehicle

end

User Uuazed
by
5.3k points