Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
46 changes: 46 additions & 0 deletions models/auto.rb
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"
Copy link
Member

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:

@color = args.fetch(: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)
Copy link
Member

Choose a reason for hiding this comment

The 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, number_of_wheels indicates that its a simple get method.

If you're going to set an attribute, use the =, like so:

def self.number_of_wheels= other
  @@wheel = other
end

Copy link
Author

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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. 

 
30

  • def self.number_of_wheels(wheels)

 
31

 
32

 
33

  • end

 
34
+end

On Wed, Apr 9, 2014 at 5:27 PM, PDaily [email protected] wrote:

  • @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)
    What you have written is much cleaner looking!
    What side effects would what I have written cause?

Reply to this email directly or view it on GitHub:
https://github.com/RubyoffRails/Episode6/pull/23/files#r11462390

@wheel = wheels
return @wheel
end
end


class Automobile < Vehicle
Automobile.number_of_wheels(4)
end
Copy link
Member

Choose a reason for hiding this comment

The 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
44 changes: 44 additions & 0 deletions spec/auto_spec.rb
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