Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ GEM
i18n (0.6.0)
multi_json (1.3.4)
pg (0.13.2)
pg (0.13.2-x86-mingw32)
rake (0.9.2.2)
rspec (2.10.0)
rspec-core (~> 2.10.0)
Expand All @@ -31,6 +32,7 @@ GEM

PLATFORMS
ruby
x86-mingw32

DEPENDENCIES
activerecord
Expand Down
16 changes: 15 additions & 1 deletion block_review.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
require 'bundler/setup'

require_relative 'db/setup'
Dir.glob("./**/*.rb").each {|f| require f}
Dir.glob("./models/*.rb").each {|f| require f}

auto1 = Automobile.new(color: "white", make: "Ford", model: "Explorer", year: "2000")
auto2 = Automobile.new(color: "blue", make: "Honda", model: "Accord", year: "2009")
auto3 = Automobile.new(color: "blue", make: "Honda", model: "Accord", year: "2011")

puts "An automobile has #{Automobile.number_of_wheels} tires."
puts "A motorcycle has #{Motorcycle.number_of_wheels} tires."
puts
puts "Auto 1: #{auto1.to_s}"
puts "Auto 2: #{auto2.to_s}"
puts "Auto 3: #{auto3.to_s}"
puts
puts "All vehicles: #{Vehicle.Vehicles.inspect}"
puts "All blue Honda Accords: #{Vehicle.get_favorite_combo}"
puts
puts "Serenity now!"
6 changes: 0 additions & 6 deletions config/database.yml.sample

This file was deleted.

9 changes: 9 additions & 0 deletions models/automobile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require_relative "Vehicle"

class Automobile < Vehicle

def self.number_of_wheels
@wheels = 4
end

end
9 changes: 9 additions & 0 deletions models/motorcycle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require_relative "Vehicle"

class Motorcycle < Vehicle

def self.number_of_wheels
@wheels = 2
end

end
58 changes: 58 additions & 0 deletions models/vehicle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class Vehicle

@@Vehicles = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should keep the naming for this class variable the same as other variables:

@@vehicles = []

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. I should have caught that. My Vehicles variable was bothering me, and I think this is why.


def self.get_favorite_combo
veh = @@Vehicles.select { |i| i.color == "blue" &&
i.make == "Honda" && i.model == "Accord" }
end

def self.reset
@@Vehicles = []
end

def self.number_of_wheels
@wheels = nil
end

def self.Vehicles
@@Vehicles
end

attr_reader :color, :make, :model, :year
def initialize (hash={})
@@Vehicles << self
@color = hash.fetch(:color)
@make = hash.fetch(:make)
@model = hash.fetch(:model)
@year = hash.fetch(:year)
end

def update (hash={})
@color = hash.fetch(:color)
@make = hash.fetch(:make)
@model = hash.fetch(:model)
@year = hash.fetch(:year)
end

def color
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for these, I prefer to create the 'getter' in ruby like so:

attr_reader :color

This will be functionality equivalent to the

def color
  @color
end

And you can simplify:

attr_reader :color, :make, :model, :year

@color
end

def make
@make
end

def model
@model
end

def year
@year
end

def to_s
"#{year} #{color} #{make} #{model}"
end

end
10 changes: 10 additions & 0 deletions spec/automobile_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "rspec"
require_relative "../models/automobile"

describe Automobile do

it "should have four wheels" do
Automobile.number_of_wheels.should eq(4)
end

end
10 changes: 10 additions & 0 deletions spec/motorcycle_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "rspec"
require_relative "../models/motorcycle"

describe Motorcycle do

it "should have two wheels" do
Motorcycle.number_of_wheels.should eq(2)
end

end
52 changes: 52 additions & 0 deletions spec/vehicle_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require "rspec"
require_relative "../models/vehicle"

describe Vehicle do

before(:each) do
Vehicle.reset
end

it "should have four wheels" do
Vehicle.number_of_wheels.should eq(nil)
end

it "class should keep track of the vehicles made" do
Vehicle.Vehicles.should_not be(nil)
Vehicle.Vehicles.count.should eq(0)
veh = Vehicle.new(color: "white", make: "Honda", model: "Accord", year: "2002")
Vehicle.Vehicles.count.should eq(1)
end


it "should take input to describe the automobile" do
auto = Vehicle.new(color: "black", make: "Honda", model: "Accord", year: "2002")
auto.color.should eq("black")
auto.make.should eq("Honda")
auto.model.should eq("Accord")
auto.year.should eq("2002")
end

it "should allow an update to the automobile" do
auto = Vehicle.new(color: "silver", make: "Toyota", model: "Camry", year: "2012")
auto.update(color: "red", make: "Honda", model: "Accord", year: "2002")
auto.color.should eq("red")
auto.make.should eq("Honda")
auto.model.should eq("Accord")
auto.year.should eq("2002")
end

it "should filter the vehicles list to only show blue Honda Accords" do
auto1 = Vehicle.new(color: "blue", make: "Honda", model: "Accord", year: "2002")
auto2 = Vehicle.new(color: "blue", make: "Honda", model: "Accord", year: "2005")
auto3 = Vehicle.new(color: "silver", make: "Toyota", model: "Camry", year: "2012")
results = Vehicle.get_favorite_combo
results.count.should eq(2)
end

it "should allow the class to be reset" do
auto = Vehicle.new(color: "silver", make: "Toyota", model: "Camry", year: "2012")
Vehicle.reset
Vehicle.Vehicles.count.should eq(0)
end
end