-
Notifications
You must be signed in to change notification settings - Fork 27
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 and Tiger Submission #29
base: master
Are you sure you want to change the base?
Conversation
end | ||
end | ||
network.shows.each do |show| | ||
puts show if show.day_of_week.downcase == day_of_week |
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.
Excellent use of network.shows
:)
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.
If you have an array where you want to do something to each element, I'd rather you limit your collection first, rather than loop over everything and selectively do something. It will lead to blocks that you can reuse and extract to methods, and it reads your intent easier.
network.shows.each do |show|
puts show if show.day_of_week.downcase == day_of_week
end
vs
network.shows.select{ |show| show.day_of_week.downcase == day_of_week }.each do |show|
puts show
end
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.
...limit your collection first, rather than loop over everything and selectively do something. It will lead to blocks that you can reuse and extract to methods, and it reads your intent easier.
That makes a ton of sense. Thanks!
Would you recommend assigning network.shows.select{ |show| show.day_of_week.downcase == day_of_week }
to a variable for readability? That line is 87 chars.
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.
Yes, that would work. Or you can have a method:
def for_day_of_week(shows, day)
shows.select{ |show| show.day.downcase == day }
end
for_day_of_week(network.shows, day_of_week).each do |show|
puts show
end
And, we could get even fancier with the last. The naming "for_day_of_week" could be "each_day_of_week", implying we give it a block (though it is fairly advanced, and you'd only do this if you needed to re-use each_day_of_week)
def each_day_of_week(shows, day)
shows.select{ |show| show.day.downcase == day }.each do |show|
yield show
end
end
each_day_of_week(network.shows, day_of_week) do |show|
puts show
end
@jwo added my Eagle level submission. Thanks! |
@@ -5,11 +5,22 @@ | |||
Dir.glob('./models/*').each { |r| require r} | |||
require "./db/seed" | |||
|
|||
puts "There are #{Show.count} in the database" | |||
i=0 |
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.
Dont think you need to keep this, right?
No description provided.