Skip to content

Instantly share code, notes, and snippets.

@jheg
Created October 22, 2014 13:24
Show Gist options
  • Save jheg/6c5569eaf2e7f05144e7 to your computer and use it in GitHub Desktop.
Save jheg/6c5569eaf2e7f05144e7 to your computer and use it in GitHub Desktop.
counter
module Drive
def speed_up( num )
self.speed += num
puts "you accelerate by #{num} and are now travelling at #{speed} mph."
end
def slow_down( num )
self.speed -= num
puts "You decrease your speed by #{num} and are now travelling at #{speed}"
end
def stop
puts "stop and park"
end
end
class Vehicle
@@num_of_vehicles = 0
def initialize
@@num_of_vehicles += 1
end
def self.mpg( miles, gallon )
puts "Your car gets #{miles / gallon}"
end
def self.info
@@num_of_vehicles
end
end
class MyCar < Vehicle
attr_accessor :year, :colour, :model, :speed
VEHICLE_TYPE = "car"
@@num_of_cars = 0
def initialize(y,c,m)
@year = y
@colour = c
@model = m
@speed = 0
@@num_of_cars += 1
end
def self.total_num_of_cars
@@num_of_cars
end
def change_car( y,c,m )
self.year = y
self.colour = c
self.model = m
end
def my_car_info
puts "You drive a #{colour} #{year} #{model}. The vehicle type is #{VEHICLE_TYPE}"
end
def to_s
puts "#{model} cars rock!"
end
include Drive
end
class MyTruck < Vehicle
VEHICLE_TYPE = "truck"
def info
puts "Your vehicle type is #{VEHICLE_TYPE}"
end
end
class MyBoat < Vehicle
end
ford = MyCar.new( "2000", "black", "Ford Modeo" )
vic = MyCar.new( "2000", "red", "Vauxhall Corsa" )
puts MyCar.total_num_of_cars
lorry1 = MyTruck.new
lorry2 = MyTruck.new
car1 = MyCar.new("2001", "black", "VW Polo GTI")
boat1 = MyBoat.new
puts Vehicle.info
puts MyCar.total_num_of_cars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment