Skip to content

Instantly share code, notes, and snippets.

@droberts-sea
Last active August 26, 2016 18:08
Show Gist options
  • Save droberts-sea/17099a4d34617e961799a6080621ef18 to your computer and use it in GitHub Desktop.
Save droberts-sea/17099a4d34617e961799a6080621ef18 to your computer and use it in GitHub Desktop.
Bicycle example
module Transportation
class Vehicle
def vechicle_method()
puts "I am a vehicle"
end
end
class Bicycle < Vehicle
attr_accessor :gear, :speed
def initialize()
@gear = 5
@speed = 0
end
def pedal()
@speed += 1
end
def brake()
@speed -= 1
end
def vechicle_method()
puts "Bicycle vehicle method"
end
end
end
class MountainBike < Transportation::Bicycle
attr_accessor :seat_height
def initialize()
super()
@seat_height = 4
end
def adjust_seat(amount)
@seat_height += amount
end
def brake()
# Mountain bikes have disc brakes
@speed -= 3
end
def vechicle_method()
super()
end
end
mb = MountainBike.new()
puts "speed: #{mb.speed}"
mb.pedal
puts "speed: #{mb.speed}"
mb.brake
puts "speed: #{mb.speed}"
puts "seat height: #{mb.seat_height}"
mb.adjust_seat(8)
puts "seat height: #{mb.seat_height}"
mb.vechicle_method
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment