I want to create a class that can simulate a car. I'll list out the specs first and then give step by step instructions on how to attack the problem.
touch car.rb
subl .
to open up your text editorcar.rb
In car.rb
, create a new class called Car
. For now, we'll have no content in it.
Load the class in IRB and create an instance of the car class.
Great! The printed message is weird and cryptic, so let's change that up.
Create a method called get_info
that sends back the message "I'm a car!". You can explicitly send back a message by typing return "I'm a car!"
. Try that out.
get_info
method on itRuby is full of shortcuts though. A Ruby method will automatically send back the result of the last line of code in the method. If you get rid of the keyword return, you'll get the same result.
The code you wrote should print out "I'm a car!" to the console now.
Now let's create a method called setup. This method should set 2 instance variables.
@fuel
to 10@distance
to 0We can also edit the get_info
method to give some info about the fuel and distance. Example:
"I'm a car. I've driven 20 miles and have 5 gallons of gas left."
To make sure @fuel
and @distance
have values, run the setup
method immediately after creating the object, but before printing it.
I don't know about you, but I have a major problem with this setup (Excuse the pun). But really, if someone wants to use our class, they would have to know that they can't use the method get_info
without running setup first. Keeping arbitrary rules for your classes is a bad practice!
Lucky for us, there is a special Ruby method, that if defined, will run every single time you create a new instance. This method is called initialize
When you call Car.new
, the object that it returns will run the method initialize
automatically, so you don't have to call it manually. In our case, we can just rename the method setup as initialize and delete the call to setup.
Inside your newly named method initialize include a puts statement (puts "the initialize method is running automatically"
).
Try it out by running the Ruby code in IRB.
Let's take the car for a spin! Create the drive
method which should take an input for the number of miles driven. Change the position accordingly and reduce the fuel at a rate of 20.0 miles per gallon.
To test this out:
car_a
, car_b
.car_a
10 milescar_b
hasn't changed and that car_a
has the proper changesWhat happens when you try to drive 500 miles? Prevent that from happening. Make it so that you can drive up until you reach 0 gallons and then print out a message saying you're out of gas and need to fuel up.
Create a fuel-up
method, which figures out how much gas you need to fill into order to reach a full tank at 10 gallons. Fill it up and print a message telling you how much it'll cost. The cost of gas is $3.50/gallon.
Test this out too.
In terminal git add
the new file and commit it
Sweet! We have our first functional class! Give yourself a pat on the back and take a break for 5 minutes.