Conversation
lib/collection.rb
Outdated
There was a problem hiding this comment.
I feel like with all of the wiz-bang stuff in Ruby "decompose this array into multiple sub arrays based on a block's evaluation" should be easier, but I couldn't find out to do it without the recursion in separate_by_years and array_of_years. Any suggestions?
There was a problem hiding this comment.
I think one of the reasons you're having trouble is that array feels too difficult here. I think hash is the appropriate data type here.
def movies_by_year(movies)
hash = {}
movies.each do |movie|
hash[movie.year] ||= []
hash[movie.year] << movie
end
hash
endok, so now you have a hash by year, which is easier to deal with.
In ruby, the data-type most often reached for is the hash. arrays are generally pretty stupid and simple lists.
There was a problem hiding this comment.
Duh. That makes a lot more sense. Let me refactor this stuff and let you take another look. Thanks.
|
Looks entirely technically correct, good job! Do you want to re-work the Collection class with the hash? otherwise, here are some style notes:
|
With the Tiger discussion, I couldn't tell whether you really wanted the "average year" or the "average in a year", so I did both. :-)
There's a couple of spots I have comments/questions about that I'll add inline.