Question

I'm trying to store the team names in either @wins or @loss depending on the home and away scores associated with the respective key.

@leaderboard_info = [{
                      home_team:"Patriots",
                      away_team: "Broncos",
                      home_score: 7,
                      away_score: 3
                      }, 
                      #more info in hashes.........]

@wins = []
@loss = []

@leaderboard_info.each do |game|
  game.each do |key,value|
    if value[:home_score] > value[:away_score]  #7 > 3
      @win << value[:home_team]   #Patriots
      @loss << value[:away_team]  #Broncos 
    else
      @loss << value[:home_team]
      @win << value[:away_team]
    end
  end
end

But I keep running into this error

[]': no implicit conversion of Symbol into Integer (TypeError)

The if statement should be grabbing the specific values of 7 and 3. Afterwards it should be pushing the team names stored by the value. Why isn't it working? I tried it with key[home_score] etc. but it still doesn't work.

Was it helpful?

Solution

I think you should remove your double loop and change it to looks like:

@leaderboard_info.each do |game|
  if game[:home_score] > game[:away_score]
    @win << game[:home_team]
    @loss << game[:away_team]
  else
     @loss << game[:home_team]
     @win << game[:away_team]
  end
end

OTHER TIPS

The issue is that you're calling each on a Hash, which yields (as your code points out) a key and value for each record in the Hash. So when you call value[:home_team], you're using the [] method on a String ("Patriots"). The [] method on String expects a Fixnum (for breaking up a String).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top