-
-
Save seaseng/5775931 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# module Vehicle | |
# attr_reader :color, :wheels, :num_seats | |
# attr_accessor :status | |
# def post_initialize(args) | |
# @color = args[:color] | |
# @wheels = args[:wheels] | |
# @status = :stopped | |
# @num_seats = args[:num_seats] | |
# end | |
# def drive | |
# self.status = :driving | |
# end | |
# def brake | |
# self.status = :stopped | |
# end | |
# def needs_gas? | |
# return [true,true,false].sample | |
# end | |
# end | |
# class Car | |
# include Vehicle | |
# def initialize(args) | |
# # puts "Car:initialize -- #{args[:color]}" | |
# post_initialize(args) | |
# end | |
# end | |
# class Bus | |
# include Vehicle | |
# attr_accessor :passengers | |
# attr_reader :fare | |
# def initialize(args) | |
# post_initialize(args) | |
# @fare = args[:fare] | |
# @passengers=[] | |
# end | |
# def drive | |
# return self.brake if stop_requested? | |
# self.status = :driving | |
# end | |
# def admit_passenger(passenger,money) | |
# self.passengers << passenger if money > self.fare | |
# end | |
# def stop_requested? | |
# return [true,false].sample | |
# end | |
# def needs_gas? | |
# return [true,true,true,false].sample | |
# end | |
# end | |
# class Motorbike | |
# include Vehicle | |
# attr_accessor :speed | |
# def initialize(args) | |
# post_initialize(args) | |
# end | |
# def drive | |
# self.status = :driving | |
# self.speed = :fast | |
# end | |
# def weave_through_traffic | |
# self.status = :driving_like_a_crazy_person | |
# end | |
# def needs_gas? | |
# false | |
# end | |
# def wheelie | |
# "Motobike Wheeelie!!!" | |
# end | |
# end | |
# puts '*'*25 | |
# puts "VEHICLE WITH MODULES" | |
# puts '*'*25 | |
# puts "" | |
# puts "Car" | |
# car_hash = Hash[:color, 'blue', :wheels, 4, :num_seats, 5] | |
# car1 = Car.new(car_hash) | |
# p "Color: #{car1.color}" | |
# p "Wheels: #{car1.wheels}" | |
# p "Status: #{car1.status}" | |
# p car1.drive == :driving | |
# p car1.brake == :stopped | |
# p car1.needs_gas? | |
# puts '-'*20 | |
# puts "" | |
# puts "Bus" | |
# bus_hash = Hash[:color, 'white', :wheels, 4, :num_seats, 50, :fare, 5] | |
# bus1 = Bus.new(bus_hash) | |
# p "color: #{bus1.color}" | |
# p "Wheels: #{bus1.wheels}" | |
# p "Num Seats: #{bus1.num_seats}" | |
# p "Fare: #{bus1.fare}" | |
# p "Status: #{bus1.status}" | |
# p bus1.drive | |
# p bus1.status == :driving | |
# # p car1.status | |
# p bus1.brake | |
# p bus1.status == :stopped | |
# p bus1.admit_passenger('Navid', 6).first == 'Navid' | |
# p bus1.stop_requested? | |
# p bus1.needs_gas? | |
# puts '-'*20 | |
# puts "" | |
# puts "Motobike" | |
# motobike = Hash[:color, 'black', :wheels, 2, :num_seats, 1] | |
# moto_bike1 = Motorbike.new(motobike) | |
# p "color: #{moto_bike1.color}" | |
# p "Wheels: #{moto_bike1.wheels}" | |
# p "Num Seats: #{moto_bike1.num_seats}" | |
# p "Status: #{moto_bike1.status}" | |
# p moto_bike1.drive | |
# p moto_bike1.status == :driving | |
# p moto_bike1.brake | |
# p moto_bike1.status == :stopped | |
# p moto_bike1.needs_gas? | |
# p moto_bike1.weave_through_traffic == :driving_like_a_crazy_person | |
# p moto_bike1.wheelie | |
# puts "" | |
class Vehicle | |
attr_reader :color, :wheels, :num_seats | |
attr_accessor :status | |
def initialize(args) | |
@color = args[:color] | |
@wheels = args[:wheels] | |
@status = :stopped | |
@num_seats = args[:num_seats] | |
end | |
def post_initialize(args) | |
end | |
def drive | |
self.status = :driving | |
end | |
def brake | |
self.status = :stopped | |
end | |
def needs_gas? | |
return [true,true,false].sample | |
end | |
end | |
class Car < Vehicle | |
def initialize(args) | |
super | |
post_initialize | |
end | |
end | |
class Bus < Vehicle | |
attr_accessor :passengers | |
attr_reader :fare | |
def initialize(args) | |
super | |
@fare = args[:fare] | |
@passengers=[] | |
end | |
def drive | |
return self.brake if stop_requested? | |
self.status = :driving | |
end | |
def admit_passenger(passenger,money) | |
self.passengers << self.passenger if money > self.fare | |
end | |
def stop_requested? | |
return [true,false].sample | |
end | |
def needs_gas? | |
return [true,true,true,false].sample | |
end | |
end | |
class Motorbike < Vehicle | |
attr_accessor :speed | |
def initialize(args) | |
super | |
end | |
def drive | |
self.status = :driving | |
self.speed = :fast | |
end | |
def weave_through_traffic | |
self.status = :driving_like_a_crazy_person | |
end | |
def needs_gas? | |
false | |
end | |
def wheelie | |
"Motobike Wheeelie!!!" | |
end | |
end | |
puts '*'*25 | |
puts "VEHICLE WITH INHERITANCE" | |
puts '*'*25 | |
puts "" | |
puts "Car" | |
car_hash = Hash[:color, 'blue', :wheels, 4, :num_seats, 5] | |
car1 = Car.new(car_hash) | |
p "Color: #{car1.color}" | |
p "Wheels: #{car1.wheels}" | |
p "Status: #{car1.status}" | |
p car1.drive == :driving | |
p car1.brake == :stopped | |
p car1.needs_gas? | |
puts '-'*20 | |
puts "" | |
puts "Bus" | |
bus_hash = Hash[:color, 'white', :wheels, 4, :num_seats, 50, :fare, 5] | |
bus1 = Bus.new(bus_hash) | |
p "color: #{bus1.color}" | |
p "Wheels: #{bus1.wheels}" | |
p "Num Seats: #{bus1.num_seats}" | |
p "Fare: #{bus1.fare}" | |
p "Status: #{bus1.status}" | |
p bus1.drive | |
p bus1.status == :driving | |
# p car1.status | |
p bus1.brake | |
p bus1.status == :stopped | |
p bus1.admit_passenger('Navid', 6).first == 'Navid' | |
p bus1.stop_requested? | |
p bus1.needs_gas? | |
puts '-'*20 | |
puts "" | |
puts "Motobike" | |
motobike = Hash[:color, 'black', :wheels, 2, :num_seats, 1] | |
moto_bike1 = Motorbike.new(motobike) | |
p "color: #{moto_bike1.color}" | |
p "Wheels: #{moto_bike1.wheels}" | |
p "Num Seats: #{moto_bike1.num_seats}" | |
p "Status: #{moto_bike1.status}" | |
p moto_bike1.drive | |
p moto_bike1.status == :driving | |
p moto_bike1.brake | |
p moto_bike1.status == :stopped | |
p moto_bike1.needs_gas? | |
p moto_bike1.weave_through_traffic == :driving_like_a_crazy_person | |
p moto_bike1.wheelie | |
puts "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment