Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialKevin Jenkins
12,630 PointsIs there a reason to include Enumerable instead of using game1.player.any?
We include Enumerable and write an each method for the Game class so we can use game1.any? to search through players.
Why not just use game1.players.any?
2 Answers
Garrett Carver
14,681 PointsI think that if you used game1.player.any?{|var| block}
, it will just return true
if the block ever evaluates to true for any of players in the array.
On the other hand, defining the each method will let you do any action you define. Like the example in the notes:
game1.each do |player|
puts "Player: #{player.name}, score: #{player.score}"
end
Does that make sense?
Eli E
10,142 Pointscouldn't you just use an if statement and avoid all of this?