Conversation
|
@jwo Eagle level has so much Math... |
|
@jwo Some questions around RSpec before I continue with the Eagle level:
|
Generally this means your tests have side-effects, and generally should not happen. Is this happening now on your pull-request?
Seems you wouldn't need to -- you could just check the average-rating before calling anything and make sure it's 0, or that it throws an exception.
Maybe? But usually you don't want to think about internal instance variables. It's a sign that you should pass in a data-object --- to separate your data from your data-methods. |
spec/movie_spec.rb
Outdated
There was a problem hiding this comment.
Movie.new will always give you a new object.
|
@jwo Hmmm. I'm using Movie#build to keep track of the Movies searched. |
I see you're storing the movies in a class variable. You may want to move this to another class, like "MovieLibrary"
Maybe try older movies: |
|
@jwo Gotcha. Am I keeping track of just the searched movies in MovieLibrary? |
yep, you have an excellent collection of searching capabilities in Ruby. The API should just give you some data to slice and dice. your final output looks like a fine start |
|
@jwo How do I create a mock Movie instance in order to check if it is cataloged in the libray? Is there a rule of thumb regarding when to mock/stub? |
|
you would stub
|
|
@jwo You've kinda lost me. |
lib/api.rb
Outdated
There was a problem hiding this comment.
I don't really know why this method exists.
|
You're getting NaN because you have two repositories. Movies' class variable, and the MovieLibrary. Simplify that to 1 and your problems should go away. |
|
@jwo Movie class looks a lot cleaner/readable. |
|
For the future, please include the full exception message -- it gives you a hell of a lot of information: |
|
What happens when you divide by 0 in real life? |
|
@jwo Hmmmmm. |
|
If you have a specification that it should return 0, then you'll need to pre-check: def average_rating
return 0 if movie_count == 0
all_movies.inject(:)/ all_movies.size
end |
|
@jwo Ditto on the full exception message. |
|
@jwo The yearly average is calculating correctly but I feel there some refactoring needed below... |
|
@jwo Refactored it to. Is the use |
|
I think you could get away without it: def average_rating_by_year(year)
average_rating all_movies.select {|movie| movie.year == year}
end
def average_rating(movies)
return 0 if count == 0
movies.inject(0.0) { |sum, movie| sum + movie.score } / movies.size
end |
|
@jwo Wow. |
|
@jwo Slope calculation added as: I feel |
|
I think it could be an input to the method. Right now, def calculate_slope(catalog)
first = catalog.first
last = catalog.last
(first.score - last.score).to_f / (last.year - first.year).to_f
end
def sort_by_year (movies)
movies.sort { |movie,another_movie| movie.year <=> another_movie.year }
endThe only thing is that the slope should already be calculated when calling it. |
|
@jwo Got it! |
|
@jwo Added a program summary. And preventing NaN errors when calculating a slope for a single movie. Is |
|
Is this good Ruby? unless movie.nil?
movie_library.catalog(movie)
puts "Found: #{movie.title}. Score: #{movie.score}"
endThe intensions are good. However, I would invoke a rule where you only use "unless" as a 1 statement modifier: # Good
bank_account.withdraw_funds! unless bankrupt?
# Bad
unless bankrupt?
bank_account.withdraw_funds!
endSo, if you switch to this, it'll read easier. if movie
movie_library.catalog(movie)
puts "Found: #{movie.title}. Score: #{movie.score}"
end |
That works... though you could achieve the same if you did return 0 if catalog.count <= 1 |
lib/movie_library.rb
Outdated
There was a problem hiding this comment.
Why are you using instance variables here?
|
@jwo I renamed the #catalog to #add and moved the instance variable setting. Set instances like so: Better? |
|
As I'm reading the code, I am wondering if there is significance in the If there is no significance, then I try to reduce down to the essence def add(movie)
movies << movie
calculate_slope sort_by_year(movies)
end |
MUCH. yay! |
|
@jwo Awesome! |
|
@jwo |
|
Sure, check this example out: http://rubyfiddle.com/riddles/8f294 |
|
Of course, this calculate_scope(sort_by_year(movies))Just more parens |
|
@jwo Hmmmmm. Your code looks great! |

@jwo Submission of Panda and Tiger levels.