-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Vehicle, Automobile & Motorcycle classes with required options #23
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class Vehicle | ||
attr_accessor :color, :make, :model, :year, :wheel | ||
## | ||
# Initialize new vehicle; if no arguments are provided, default to "None" | ||
def initialize(args) | ||
@color = args[:color] ||= "None" | ||
@make = args[:make] ||= "None" | ||
@model = args[:model] ||= "None" | ||
@year = args[:year] ||= "None" | ||
end | ||
## | ||
# If I want to update the Vehicle, so shall it be | ||
def updater(args) | ||
@color = args.fetch(:color) if args[:color] | ||
@make = args.fetch(:make) if args[:make] | ||
@model = args.fetch(:model) if args[:model] | ||
@year = args.fetch(:year).to_s if args[:year] | ||
end | ||
## | ||
# Such wheels, so round | ||
# May have been overzealous | ||
# | ||
# Could have done: | ||
# def number_of_wheels | ||
# 4 | ||
# end | ||
# | ||
# But wanted to mess with arguments | ||
## | ||
def self.number_of_wheels(wheels) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method has what you'd call "side-effects"... but the method name, If you're going to set an attribute, use the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What you have written is much cleaner looking! What side effects would what I have written cause? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this method you set '@wheels=wheels' You would not normally expect that. Since it's something else that happens, we call that a side effect.
On Wed, Apr 9, 2014 at 5:27 PM, PDaily [email protected] wrote:
|
||
@wheel = wheels | ||
return @wheel | ||
end | ||
end | ||
|
||
|
||
class Automobile < Vehicle | ||
Automobile.number_of_wheels(4) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll notice above I used "@@wheels" -- this indicates a class variable... Or a variable for the entire Automobile class. to set it: class Automobile < Vehicle
@@wheel = 4
end And to be able to return its value: class Automobile < Vehicle
@@wheel = 4
def self.wheel
@@wheel
end
end
Automobile.wheel
=> 4 |
||
|
||
|
||
## | ||
# Doesn't have a redefined method, but should still give us correct wheels. | ||
class Motorcycle < Vehicle | ||
Motorcycle.number_of_wheels(2) | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require './models/auto' | ||
require 'rspec' | ||
|
||
describe Automobile do | ||
|
||
let(:auto) {Automobile.new(color: "Red",make: "Ford",model: "Fiesta",year: "2014")} | ||
|
||
## | ||
# Passing tests | ||
it "should have a color" do | ||
auto.color.should eq("Red") | ||
end | ||
it "should have a make" do | ||
auto.make.should eq("Ford") | ||
end | ||
it "should have a model" do | ||
auto.model.should eq("Fiesta") | ||
end | ||
it "should have a year made" do | ||
auto.year.should eq("2014") | ||
end | ||
it "should be able to update via hash" do | ||
auto.updater(color: "Blue",make: "Chevy", model: "Nova SS", year: "1968") | ||
auto.color.should eq("Blue") | ||
auto.make.should eq("Chevy") | ||
auto.model.should eq("Nova SS") | ||
auto.year.should eq("1968") | ||
end | ||
it "should have 4 the wheels" do | ||
Automobile.number_of_wheels(4).should eq(4) | ||
end | ||
it "should not have 300 wheels" do | ||
Automobile.number_of_wheels(300).should_not eq(4) | ||
end | ||
end | ||
|
||
describe Motorcycle do | ||
it "should have 2 wheels" do | ||
Motorcycle.number_of_wheels(2).should eq(2) | ||
end | ||
it "should not have 4 wheels" do | ||
Motorcycle.number_of_wheels(4).should_not eq(2) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works, but I prefer using "fetch" in this circumstance: