Created
November 14, 2012 21:01
-
-
Save collinschaafsma/4074782 to your computer and use it in GitHub Desktop.
bdw OO
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
# OO for the win! | |
# Bike class | |
# Classes in Ruby are Capitalized and are defined like so... | |
class Bike | |
# Accessors are handy, this is the equivalant of | |
# | |
# def state | |
# @state | |
# end | |
# | |
# def state=(state) | |
# @state = state | |
# end | |
# | |
attr_accessor :motion, :gear | |
# Every Ruby class has an initialize method that is fired | |
# when we instantiate the class. Here we are creating our own | |
# initialzer method so we can do cool stuff. | |
def initialize(gear = 24, state = 'stopped') | |
@motion = state | |
@gear = gear | |
end | |
# I'm an instance method, you need to instantiate | |
# the Bike class before you can call me! | |
def pedal | |
@motion = 'pedaling' | |
end | |
def coast | |
@motion = 'coasting' | |
end | |
def brake | |
@motion = 'braking' | |
puts "Braking..." | |
sleep 1 | |
@motion = 'stopped' | |
end | |
# I'm a class method (notice self. in front of the method name), | |
# you don't need to instantiate / create a new instance of Bike | |
# to call me. | |
def self.description | |
"I'm a bike class you can configure and ride!" | |
end | |
def wheel_size | |
"I'm not sure" | |
end | |
end | |
# puts "Calling class method .description" | |
# puts Bike.description | |
# # Let's create a new instance of the Bike class | |
my_bike = Bike.new | |
puts "Currently I'm #{my_bike.motion}." | |
puts my_bike.gear | |
# my_rad_bike = Bike.new("coasting") | |
# my_bike.pedal | |
# puts "Now I'm #{my_bike.state}." | |
# puts "Now I'm rad #{my_rad_bike.state}." | |
# my_bike.coast | |
# puts "Now I'm #{my_bike.state}." | |
# my_bike.brake | |
# puts "Now I'm #{my_bike.state}." | |
# # # Now let's make a Mountain bike that "inherits" our Bike class | |
class MountainBike < Bike | |
def pedal | |
puts "Pedaling on some sick singletrack!" | |
super | |
end | |
def wheel_size | |
"650B" | |
end | |
def front_fork | |
"White Brothers Loop" | |
end | |
end | |
# # # Let's create a new instance of the MountainBike class that inherits the Bike class | |
my_mtb = MountainBike.new | |
my_mtb.pedal | |
puts "Still #{my_mtb.motion}." | |
# puts "I'm rolling on #{my_mtb.wheel_size}'s!" | |
# puts "My #{my_mtb.front_fork} is eating it!!" | |
# puts "This is gonna break" | |
# my_bike.front_fork |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment