Created
June 18, 2010 17:40
-
-
Save paddor/443950 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
#! /usr/local/bin/ruby | |
class Car | |
# excetion raised when not enough fuel left to drive given distance | |
class NotEnoughFuel < RuntimeError | |
end | |
# constructor with one parameter | |
def initialize vendor | |
@vendor = vendor # remember the vendor | |
@fuel = 50 # initial amount of fuel | |
end | |
# create getter method for fuel | |
attr_reader :fuel | |
# manually create getter method for vendor | |
def vendor | |
@vendor | |
end | |
def empty? | |
@fuel == 0 # returns true if @fuel is 0 | |
end | |
def fill! | |
@fuel = 50 | |
end | |
# Drives _miles_ miles and returns the needed fuel. | |
def drive miles | |
# this car drives 10 miles per fuel unit | |
needed_fuel = miles / 10.0 | |
raise NotEnoughFuel if @fuel < needed_fuel | |
@fuel -= needed_fuel | |
puts "Car just drove #{miles} miles and needed #{needed_fuel} fuel." | |
return needed_fuel # 'return' is optional here | |
end | |
end | |
# if this file was executed on its own (not loaded as a library) | |
if $0 == __FILE__ | |
my_car = Car.new "Ford" | |
my_car.drive 55 | |
puts "Fuel left: #{my_car.fuel}" | |
begin | |
# do forever | |
loop do | |
# will raise after a few runs... | |
my_car.drive 38 | |
end | |
rescue Car::NotEnoughFuel | |
puts "Oh shit! Not enough fuel!" | |
end | |
puts "The end." | |
end |
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
# Run the command "irb" in the Terminal app and do the following. | |
# ">>" lines were typed. | |
# "=>" lines are return values. | |
>> require 'car' | |
=> true | |
>> brum = Car.new 'Fiat' | |
=> #<Car:0x1011ce010 @fuel=50, @vendor="Fiat"> | |
>> brum.drive 100 | |
Car just drove 100 miles and needed 10.0 fuel. | |
=> 10.0 | |
>> brum.fuel | |
=> 40.0 | |
>> brum.empty? | |
=> false | |
>> brum.drive 100 | |
Car just drove 100 miles and needed 10.0 fuel. | |
=> 10.0 | |
>> brum.fuel | |
=> 30.0 | |
>> brum.drive 300 | |
Car just drove 300 miles and needed 30.0 fuel. | |
=> 30.0 | |
>> brum.empty? | |
=> true | |
>> brum.fill! | |
=> 50 | |
>> brum.fuel | |
=> 50 | |
>> class Car | |
>> def horn | |
>> puts "Roooooaaarrrrrr!" | |
>> end | |
>> end | |
=> nil | |
>> brum.horn | |
Roooooaaarrrrrr! | |
=> nil | |
>> module RoadRunner | |
>> def meep | |
>> puts "Meep meep!" | |
>> end | |
>> end | |
=> nil | |
>> class Car | |
>> include RoadRunner | |
>> end | |
=> Car | |
>> brum.meep | |
Meep meep! | |
=> nil |
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
paddor@mcpaddor ~ ∴ ruby car.rb | |
Car just drove 55 miles and needed 5.5 fuel. | |
Fuel left: 44.5 | |
Car just drove 38 miles and needed 3.8 fuel. | |
Car just drove 38 miles and needed 3.8 fuel. | |
Car just drove 38 miles and needed 3.8 fuel. | |
Car just drove 38 miles and needed 3.8 fuel. | |
Car just drove 38 miles and needed 3.8 fuel. | |
Car just drove 38 miles and needed 3.8 fuel. | |
Car just drove 38 miles and needed 3.8 fuel. | |
Car just drove 38 miles and needed 3.8 fuel. | |
Car just drove 38 miles and needed 3.8 fuel. | |
Car just drove 38 miles and needed 3.8 fuel. | |
Car just drove 38 miles and needed 3.8 fuel. | |
Oh shit! Not enough fuel! | |
The end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment