-
Notifications
You must be signed in to change notification settings - Fork 22
Panda, tiger, eagle! #16
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
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,5 @@ | ||
| require_relative 'vehicle' | ||
|
|
||
| class Automobile < Vehicle | ||
|
|
||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| require_relative 'vehicle' | ||
|
|
||
| class Motorcycle < Vehicle | ||
| def self.num_wheels | ||
| num_wheels = 2 | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| class Vehicle | ||
|
|
||
| attr_reader :color, :make, :model, :year, :name | ||
|
|
||
| @@all_vehicles = [] | ||
|
|
||
| def initialize(args={}) | ||
| @color = args.fetch(:color) if args[:color] | ||
|
Member
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. if you're going to use If that's the case, this syntax is preferred: @color = args[:color]If you want a default value, you can do this: @color = args.fetch(:color, "red") |
||
| @make = args.fetch(:make) if args[:make] | ||
| @model = args.fetch(:model) if args[:model] | ||
| @year = args.fetch(:year).to_s if args[:year] | ||
| @name = args.fetch(:name) if args[:name] | ||
| args[:year] = @year.to_s | ||
| @@all_vehicles << args | ||
| end | ||
|
|
||
| def self.all_vehicles | ||
| @@all_vehicles | ||
| end | ||
|
|
||
| def self.num_wheels | ||
| num_wheels = 4 | ||
| end | ||
|
|
||
| def self.filter(args ={}) | ||
| search_str = args.to_a.map { |group| "vehicle[:#{group[0]}] == '#{group[1]}' and "}.reduce(:+)[0..-5] | ||
| @@all_vehicles.find_all { |vehicle| eval(search_str) } | ||
| end | ||
|
|
||
| def update_all_vehicles(name) | ||
|
Member
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. Something that can update all instances of a class should be on the class itself. That is, the method should be "scoped" to the object -- it should have the same powers as the object itself. tl;dr If it can update more than 1 thing: def self.update_all_vehicles(name) |
||
| index = @@all_vehicles.index {|vehicle| vehicle[:name] == name} | ||
|
Member
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. so, instead of using "index", in ruby you have the "find" method, and then update it in place. Vehicle.all_vehicles.find{|v| v.name == name}.update(args)Example usage: http://rubyfiddle.com/riddles/37f83 |
||
| @@all_vehicles[index] = { name: @name, color: @color, make: @make, model: @model, year: @year} | ||
| end | ||
|
|
||
| def all_vehicles | ||
| @@all_vehicles | ||
| end | ||
|
|
||
| def update(name, 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] | ||
| update_all_vehicles(name) | ||
|
Member
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 went a bit beyond the spec --- it makes sense to do this, but it wasn't required of you. Mostly, if I have a vehicle, I want to be able to update it. class Vehicle
def update(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]
self
end
end |
||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| require_relative 'spec_helper' | ||
|
|
||
| describe Automobile do | ||
|
|
||
| it "should return the number of wheels" do | ||
| expect(Automobile.num_wheels).to eq(4) | ||
| end | ||
|
|
||
| it "should have and set attributes: color, make, model, and year" do | ||
| car = Automobile.new(name: 'bill', color: "blue", make: "Toyota", model: "Prius", year: 2008 ) | ||
| expect(car.color).to eq('blue') | ||
| expect(car.make).to eq('Toyota') | ||
| expect(car.model).to eq('Prius') | ||
| expect(car.year).to eq('2008') | ||
| end | ||
|
|
||
| it "should allow you to change attributes" do | ||
| car = Automobile.new(name: 'jerry', color: "blue", make: "Toyota", model: "Prius", year: 2008 ) | ||
| expect(car.color).to eq("blue") | ||
| car.update('jerry', color: "yellow") | ||
| expect(car.color).to eq("yellow") | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| require_relative 'spec_helper' | ||
|
|
||
| describe Motorcycle do | ||
| it "should return the number of wheels" do | ||
| expect(Motorcycle.num_wheels).to eq(2) | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| require 'rspec' | ||
| require 'bundler/setup' | ||
| require_relative '../db/setup' | ||
|
|
||
| Dir.glob("./models/**/*.rb").each {|f| require f} | ||
|
|
||
|
|
||
|
|
||
| RSpec.configure do |config| | ||
| # Use color in STDOUT | ||
| config.color_enabled = true | ||
|
|
||
| # Use color not only in STDOUT but also in pagers and files | ||
| config.tty = true | ||
|
|
||
| # Use the specified formatter | ||
| config.formatter = :documentation # :progress, :html, :textmate | ||
| end | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| require_relative 'spec_helper' | ||
|
|
||
| describe Vehicle do | ||
|
|
||
| it "should add to @all_vehicles" do | ||
| Automobile.new(name: 'new', color: 'purple', make: 'Toyota', model: 'Prius', year: 2008) | ||
| index = Automobile.all_vehicles.index {|vehicle| vehicle[:name] == 'new' } | ||
| expect(Automobile.all_vehicles[index]).to eq({name: 'new', :color => 'purple', :make => 'Toyota', :model => 'Prius', :year => "2008"}) | ||
| end | ||
|
|
||
| describe "#filter" do | ||
| it "should filter on blue honda accords" do | ||
| Automobile.new(name: 'new3', color: 'blue', make: 'Honda', model: 'Accord', year: 2008) | ||
| Automobile.new(name: 'new4', color: 'white', make: 'Honda', model: 'Accord', year: 2008) | ||
| Automobile.new(name: 'new5', color: 'blue', make: 'Honda', model: 'Accord', year: 2008) | ||
| Automobile.new(name: 'new6', color: 'purple', make: 'Toyota', model: 'Prius', year: 2008) | ||
| expect(Automobile.filter(color: 'blue', make: 'Honda', model: 'Accord')).to eq([{name: 'new3', color: 'blue', make: 'Honda', model: 'Accord', year: "2008"}, {name: 'new5', color: 'blue', make: 'Honda', model: 'Accord', year: "2008"}]) | ||
| end | ||
| end | ||
|
|
||
|
|
||
| describe "#update" do | ||
| it "should update @@all_vehicles" do | ||
| car = Automobile.new(name: 'new2', color: 'purple', make: 'Toyota', model: 'Prius', year: 2008) | ||
| car.update('new2', year: 2009) | ||
| index = car.all_vehicles.index {|vehicle| vehicle[:name] == 'new2' } | ||
| expect(car.all_vehicles[index][:year]).to eq('2009') | ||
| end | ||
| end | ||
|
|
||
| it "should return the number of wheels" do | ||
| expect(Vehicle.num_wheels).to eq(4) | ||
| end | ||
|
|
||
| it "should keep track of the vehicles created" do | ||
| Automobile.new(name: 'john', color: 'yellow', make: 'Toyota', model: 'Prius', year: 2008) | ||
| Motorcycle.new(name: 'ben', color: 'red', make: 'honda', model: 'fast', year: 2014) | ||
| expect(Automobile.all_vehicles.length).to 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 is the same as