Возможно, я уже публиковал этот вложенный хэш.Этот хэш состоит из 4 уровней для каждого родительского ключа (: home,: away).Я хочу получить доступ к 4-му уровню для каждого родительского ключа, чтобы сложить все значения: points, затем я бы хотел сравнить каждое из суммированных значений и выяснить, какое из них является наибольшим значением.Я попытался сделать это самостоятельно, однако я получаю сообщение об ошибке
без неявного преобразования Symbol в целое число (repl): 136: в []'
(repl):136:in
блок в win_team '(repl): 135: в each'
(repl):135:in
victory_team '(repl): 158: in `'
Ниже приведены хэш и моя попытка этого кода, любое объяснение того, почему я получаю эту ошибку, и любые улучшения, чтобы получить желаемый результат,быть действительно полезным.Мои извинения, если это слишком грязно, понимаю.
game_hash = {
:home =>
{
:team_name => "Brooklyn Nets",
:colors => ["Black", "White"],
:players => {
:player1 => {
:name => "Alan Anderson",
:number => 0,
:shoe_size => 16,
:points => 22,
:rebounds => 12,
:assists => 12,
:steals => 3,
:blocks => 1,
:slam_dunks => 1
},
:player2 => {
:name => "Reggie Evans",
:number => 30,
:shoe_size => 14,
:points => 12,
:rebounds => 12,
:assists => 12,
:steals => 12,
:blocks => 12,
:slam_dunks => 7
},
:player3 => {
:name => "Brook Lopez",
:number => 11,
:shoe_size => 17,
:points => 17,
:rebounds => 19,
:assists => 10,
:steals => 3,
:blocks => 1,
:slam_dunks => 15
},
:player4 => {
:name => "Mason Plumlee",
:number => 1,
:shoe_size => 19,
:points => 26,
:rebounds => 12,
:assists => 6,
:steals => 3,
:blocks => 8,
:slam_dunks => 5
},
:player5 => {
:name => "Jason Terry",
:number => 31,
:shoe_size => 15,
:points => 19,
:rebounds => 2,
:assists => 2,
:steals => 4,
:blocks => 11,
:slam_dunks => 1
}
},
:away =>
{
:team_name => "Charlotte Hornets",
:colors => ["Turquoise", "Purple"],
:players => {
:player1 => {
:name => "Jeff Adrien",
:number => 4,
:shoe_size => 18,
:points => 10,
:rebounds => 1,
:assists => 1,
:steals => 2,
:blocks => 7,
:slam_dunks => 2
},
:player2 => {
:name => "Bismak Biyombo",
:number => 0,
:shoe_size => 16,
:points => 12,
:rebounds => 4,
:assists => 7,
:steals => 7,
:blocks => 15,
:slam_dunks => 10
},
:player3 => {
:name => "DeSagna Diop",
:number => 2,
:shoe_size => 14,
:points => 24,
:rebounds => 12,
:assists => 12,
:steals => 4,
:blocks => 5,
:slam_dunks => 5
},
:player4 => {
:name => "Ben Gordon",
:number => 8,
:shoe_size => 15,
:points => 33,
:rebounds => 3,
:assists => 2,
:steals => 1,
:blocks => 1,
:slam_dunks => 0
},
:player5 => {
:name => "Brendon Haywood",
:number => 33,
:shoe_size => 15,
:points => 6,
:rebounds => 12,
:assists => 12,
:steals => 22,
:blocks => 5,
:slam_dunks => 12
}
}
}
}
}
##winning team
def winning_team(game_data)
#finding the sum of all :points in :home
game_data[:home].each do |attribute, value|
value[:players].each do |player, player_data|
score_1 = player_data.find{|k| k[:points]}.sum
end
end
#finding the sum of all :points in :away
game_data[:away].each do |attribute, value|
value[:players].each do |player, player_data|
score_2 = player_data.find{|k| k[:points]}.sum
end
end
#outputting a final statement of who won the game
if score_1 < score_2
puts "Brooklyn Nets won the game"
else
puts "Charlotte Hornets won the game"
end
end