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 trialNiristotle Okram
2,000 PointsQuestion on the example enumerable.rb program in treehouse
so in the topic https://teamtreehouse.com/library/ruby-modules/ruby-core-modules/enumerable, there was the program with 2 classes. In the Game
class, there is this method for the 'score'
def score
score = 0
players.each do |player|
score += player.score
end
score
end
Question: how is the score value retrieved by this method?.
This is what i understand so far, players in an array and for the game1 (in this case), the players will be an array that have the hashes inside like this
players= [{Jason: , 100}, {Kenneth: ,80}]
Update: how does the player1 & player2 data looks like thats is being pushed to the array players.
1 Answer
Zachary Fine
10,110 Pointsplayers= [{Jason: , 100}, {Kenneth: ,80}] //Delete your commas.
This is an array of objects. An object looks like this: jason = {Jason:100} jason[:jason] = 100
Niristotle Okram
2,000 PointsNiristotle Okram
2,000 PointsAn array is always a collections of objects.
Zachary Fine
10,110 PointsZachary Fine
10,110 PointsNo. Think of an array as a list.
It could be a list of strings: ["cat", "dog", "steve"]
A list of numbers: [1,2,3,4]
a list of objects/hashes: [{Jim: 24},{Tom: 42},{John:27}]
Niristotle Okram
2,000 PointsNiristotle Okram
2,000 Pointsan array can be a heterogeneous like: array = [1, abc, 9.55, {"name" => "Luke"}] but whatβs the point for this to my question :)