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
46 changes: 46 additions & 0 deletions panda.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
class Vehicle
@@wheels = 4
attr_accessor :color, :make, :model

def initialize(color, make, model)
@color = color
@make = make
@model = model
end

def update(hash)
unless hash[:color].nil?
@color = hash[:color]
end
unless hash[:make].nil?
@make = hash[:make]
Copy link
Member

Choose a reason for hiding this comment

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

I would probably "in-line" this... good rule of thumb, use in-line if there is only one statement in your "if/unless"

@make = hash[:make] if hash[:make]

Or, the example before:

@make = hash.fetch(:make, @make)
### which is similar to 
@make = hash.fetch(:make) {@make}

end
unless hash[:model].nil?
@model = hash[:model]
end
end

def wheels
"This vehicle has #{@@wheels} wheels."
end
end

automobile = Vehicle.new("Red", "Porsche", "911")
puts automobile.wheels
puts "Created an automobile with the following information:
Make: #{automobile.make}
Model: #{automobile.model}
Color: #{automobile.color}"

update_hash = { :color => "Green", :make => "Toyota", :model => "Tacoma" }
automobile.update(update_hash)
puts "Updated the automobile's make model and color:
Make: #{automobile.make}
Model: #{automobile.model}
Color: #{automobile.color}"

automobile.update({:color => "Black"})
puts "Updated the automobile's color only
Make: #{automobile.make}
Model: #{automobile.model}
Color #{automobile.color}"
38 changes: 38 additions & 0 deletions tiger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Vehicle
@@wheels = 4
attr_accessor :color, :make, :model

def initialize(color, make, model)
@color = color
@make = make
@model = model
end

def update(hash)
unless hash[:color].nil?
@color = hash[:color]
end
unless hash[:make].nil?
@make = hash[:make]
end
unless hash[:model].nil?
@model = hash[:model]
end
end

def wheels
"This vehicle has #{@@wheels} wheels."
end
end

class Automobile < Vehicle

end

class Motorcycle < Automobile
def self.wheels
"This vehicle has 2 wheels."
end
end

puts Motorcycle.wheels