-
Notifications
You must be signed in to change notification settings - Fork 23
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
Panda Assignment submission. #20
base: master
Are you sure you want to change the base?
Conversation
@@ -0,0 +1,13 @@ | |||
class Automobile | |||
attr_accessor :color, :make, :model, :year | |||
def self.wheels? |
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.
a method that ends in "?" should return a true/false
Good first step at a Panda submission -- let me know if you have any questions on it, and be sure to ping me "@jwo" when you push new code for this pull request. |
Gotcha. On 4 February 2014 17:19, Jesse Wolgamott notifications@github.com wrote:
"When you are tired of being yourself then you can be ordinary." - dudu |
You should type “@jwo" On Tuesday, February 4, 2014 at 10:21 AM, drammopo wrote:
|
Corrections made. |
Looks good. The only thing missing is:
So, I should be able to: auto = Automobile.new(x,y,z)
auto.update(a,b,c) |
Added update method to the class. |
p auto | ||
auto.update(color: 'Blue', year: 2012) | ||
puts auto.color | ||
puts auto.year |
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.
what does purs auto.model
output? Is it what you expect?
What you want to do is overwrite, if the arg is there, otherwise you leave it. This is the most common form of that expression @model = new_args[:model] if new_args[:model] You could also use fetch with a default to achieve a cleaner, but possibly harder to understand look: @model = new_args.fetch(:model, @model) |
For now I'll use: @model = new_args[:model] if new_args[:model] The correction has been made. |
|
||
def update(new_args) | ||
@color = new_args[:color] if new_args[:color] | ||
@make ||= new_args[:make] if new_args[:make] |
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.
why use "||=" ?
Well spotted. |
Awesome 🎉 ! |
…all vehicles made.
@jwo Automobiles are not counted when created? All other types are. Why is that? |
Oh, should have caught that. Call "super" in your subclass, and it'll call the initialize in your superclass. So when auto is initialized, if you call super, it will call initialize of Vehicle as well. On Thu, Feb 6, 2014 at 11:39 AM, drammopo notifications@github.com
|
I had that in my original code and but got: See snippet below def initialize(args)
@color = args[:color]
@make = args[:make]
@model = args[:model]
@year = args[:year]
super
end |
Ahh try super() That's tell ruby to only call it with 0 arguments. On Thu, Feb 6, 2014 at 11:55 AM, drammopo notifications@github.com
|
Working now. |
It would seem that the following are not needed Vehicle.register(self)
Automobile.build_car(args) And you don't need both a |
To clarify, this is what I'm after: class Automobile
def initialize(args)
@color = args[:color]
@make = args[:make]
@model = args[:model]
@year = args[:year]
Vehicle.register(self)
Automobile.build_car(args)
super()
end
def self.build(args)
auto = Automobile.build(args)
@@vehicles << auto
auto
end
end
describe Automobile do
it "records the auto when I build it" do
auto = Automobile.build
expect(Automobile.all_autos).to include?(auto)
end
end |
@jwo Please expand on the Also can you call self.build on itself as per your example above? def self.build(args)
auto = Automobile.build(args)
@@vehicles << auto
auto
end |
Yes it's similar.
Yes, why do you think you couldn't? On Fri, Feb 7, 2014 at 1:37 PM, drammopo notifications@github.com wrote:
|
@jwo Hmmmm. I see the error in my thinking. class Automobile
def initialize(args)
@color = args[:color]
@make = args[:make]
@model = args[:model]
@year = args[:year]
Automobile.build(args)
super()
end
def self.build(args)
auto = Automobile.build(args)
@@vehicles << auto
auto
end
end
describe Automobile do
it "records the auto when I build it" do
auto = Automobile.build
expect(Automobile.all_autos).to include?(auto)
end
end |
Here is what I want in this discussion. You don't register when you call class Automobile
def initialize(args)
@color = args[:color]
@make = args[:make]
@model = args[:model]
@year = args[:year]
super()
end You register if someone calls That makes In the same way, in Rails, you can call |
@jwo The latest code is up. |
here's what I'd love to see Automobile.count
=> 0
Automobile.build(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007)
Automobile.count
=> 1 |
@jwo |
Well, this is an exercise in showing the difference between class methods and initialization methods. In a production system, you don't want an initializer to have side effects. You should be able to create objects without effects. Then, you can call different methods to persist or otherwise affect them. On Fri, Feb 7, 2014 at 3:41 PM, drammopo notifications@github.com wrote:
|
@jwo def self.build(args)
auto = Automobile.new(args)
@@vehicles << auto
@@auto << auto
auto
end |
awesome! On Fri, Feb 7, 2014 at 3:47 PM, drammopo notifications@github.com wrote:
|
@jwo Used the Rails operation(s) all the time without understanding what was going on under the hood. Thanks for this! |
That is literally the coolest, best compliment you can give me for this course 🎉 😄 Do you approve for me to use it in a testimonial? |
Definitely. Midnight all ready. Gotta catch some Zzzz. |
@jwo I made a pun related to cars without realising. |
|
||
|
||
before :each do | ||
@auto = Automobile.new(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007) |
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.
I would rather you use "let" instead of @auto
.
Instead of
before :each do
@auto = Automobile.new(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007)
end
I would
let(:auto) { Automobile.new(model: 'Mustang', color: 'Red', make: 'Ford', year: 2007) }
Then later,
auto.should be_an_instance_of Automobile
Test suite looks most excellent! I think you have this down (and also the longest pull request ever :) ) |
@jwo Awesome! |
@jwo Am I testing the Vehicle#search correctly? |
Can you point me to where you are testing it? |
@jwo /spec/vehicle_spec.rb |
Going to say no, vehicle_spec looks extremely bare. |
@jwo Apologies. |
Automobile.build model: :toyota, color: :red, make: 'Corolla', year: 2010 | ||
auto = Automobile.build model: :honda, color: :blue, make: 'Civic', year: 2013 | ||
Automobile.build model: :mazda, color: :blue, make: 'Accord', year: 2003 | ||
Vehicle.search(color: "blue", model: "honda").first.should eq(auto) |
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 seems like a fine search. It doesn't cover all functionality though.
I might like it if it were the following, where you are more assertive about the results.
Vehicle.search(color: "blue", model: "honda").should eq([auto])
Created a panda.rb file in the Models directory.