Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
19 changes: 17 additions & 2 deletions block_review.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@
require 'bundler/setup'

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

auto1 = Auto.new(color: 'Blue', make: 'Honda', model: "Accord", year: '2007') #self
motor1 = Motor.new(color: 'Red', make: 'Yamaha', model: "Z9", year: '2012')
puts
puts "A car has #{Auto.wheels} wheels"
puts "A motorcycle has #{Motor.wheels} wheels"
puts
puts "Auto1: #{auto1.to_s}"
puts "Motor1: #{motor1.to_s}"
puts
puts 'All Vehicles:'
puts Vehicle.vehicles
puts
puts 'Best car ever!!!'
puts Vehicle.favorite_car


puts "Serenity now!"
6 changes: 0 additions & 6 deletions config/database.yml.sample

This file was deleted.

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

class Auto < Vehicle

def self.wheels
@@wheels = 4
end
end
8 changes: 8 additions & 0 deletions models/motor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require_relative "Vehicle"

class Motor < Vehicle

def self.wheels
@@wheels = 2
end
end
29 changes: 29 additions & 0 deletions models/vehicle.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Vehicle

@@vehicle = [] #can be used anywhere.

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

def self.wheels
@wheels = nil
Copy link
Member

Choose a reason for hiding this comment

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

if you're calling a method in Ruby, you don't need to assign it to a variable. so here you could:

def self.wheels
  nil
end

end

def self.vehicles
@@vehicle
end

def self.favorite_car
@@vehicle.select { |i| i.color == 'Blue' && i.model == 'Accord'} #select returns the ary that's true to the conditon
end

def to_s
"#{color}, #{make}, #{model}, #{year}"
end
end
9 changes: 9 additions & 0 deletions spec/auto_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

require_relative "spec_helper"

describe Auto do
it "should have four wheels" do
Auto.wheels.should eq(4)
end

end
9 changes: 9 additions & 0 deletions spec/motor_spec
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

require_relative "spec_helper"

describe Motor do
it "should have four wheels" do
Motor.wheels.should eq(2)
end

end
6 changes: 6 additions & 0 deletions spec/spec_helper
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require "rspec"
require 'bundler/setup'
require_relative '../db/setup'
require_relative "../models/vehicle"
require_relative "../models/auto"
require_relative "../models/motor"
24 changes: 24 additions & 0 deletions spec/vehicle_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require_relative "spec_helper"

describe Vehicle do

it "should start with no wheels" do
Vehicle.wheels.should be_nil
end

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

it "should be able to filter the vehicles to only blue honda accords" do
auto1 = Vehicle.new(color: "Blue", make: "Honda", model: "Accord", year: "2007")
auto2 = Vehicle.new(color: "Blue", make: "Honda", model: "Accord", year: "2009")
auto3 = Vehicle.new(color: "Orange", make: "Yamaha", model: "Z7.0", year: "2019")
results = Vehicle.favorite_car
results.count.should eq(2)
end
ends