Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jperezish
Copy link

No description provided.

end
end
network.shows.each do |show|
puts show if show.day_of_week.downcase == day_of_week
Copy link
Member

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 :)

Copy link
Member

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

Copy link
Author

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.

Copy link
Member

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

@jperezish
Copy link
Author

@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
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants