1

I'm having trouble finding the largest value associated with a shoe size key in a hash. Here is what I have so far

    def biggest_shoe
      max_size = nil

      game_hash.each do |location, team_data|
        team_data[:players].each do |player, stats|
          size = stats[:shoe]
          if max_size < size
            max_size = size
            #binding.pry
          end
        end
      end
    end

I was able to successfully grab the individual value of a shoe throughout iteration with stats[:shoe] but from there I haven't figured a way to pull the largest size from the hash its iterating.

The hash:

    def game_hash
      {
        home: {
          :team_name => "Brooklyn Nets",
          :colors => ["Black", "White"],
          :players => {
            "Alan Anderson" => {
              :number => 0,
              :shoe => 16,
              :points => 22,
              :rebounds => 12,
              :assists => 12,
              :steals => 3,
              :blocks => 1,
              :slam_dunks => 1
            },
            "Reggie Evans" => {
              :number => 30,
              :shoe => 14,
              :points => 12,
              :rebounds => 12,
              :assists => 12,
              :steals => 12,
              :blocks => 12,
              :slam_dunks => 7
            },
            "Brook Lopez" => {
              :number => 11,
              :shoe => 17,
              :points => 17,
              :rebounds => 19,
              :assists => 10,
              :steals => 3,
              :blocks => 1,
              :slam_dunks => 15
            },
            "Mason Plumlee" => {
              :number => 1,
              :shoe => 19,
              :points => 26,
              :rebounds => 12,
              :assists => 6,
              :steals => 3,
              :blocks => 8,
              :slam_dunks => 5
            },
            "Jason Terry" => {
              :number => 31,
              :shoe => 15,
              :points => 19,
              :rebounds => 2,
              :assists => 2,
              :steals => 4,
              :blocks => 11,
              :slam_dunks => 1
            }
          }
        },
        away: {
          :team_name => "Charlotte Hornets",
          :colors => ["Turquoise", "Purple"],
          :players => {
            "Jeff Adrien" => {
              :number => 4,
              :shoe => 18,
              :points => 10,
              :rebounds => 1,
              :assists => 1,
              :steals => 2,
              :blocks => 7,
              :slam_dunks => 2,
            },
            "Bismak Biyombo" => {
              :number => 0,
              :shoe => 16,
              :points => 12,
              :rebounds => 4,
              :assists => 7,
              :steals => 7,
              :blocks => 15,
              :slam_dunks => 10
            },
            "DeSagna Diop" => {
              :number => 2,
              :shoe => 14,
              :points => 24,
              :rebounds => 12,
              :assists => 12,
              :steals => 4,
              :blocks => 5,
              :slam_dunks => 5
            },
            "Ben Gordon" => {
              :number => 8,
              :shoe => 15,
              :points => 33,
              :rebounds => 3,
              :assists => 2,
              :steals => 1,
              :blocks => 1,
              :slam_dunks => 0
            },
            "Brendan Haywood" => {
              :number => 33,
              :shoe => 15,
              :points => 6,
              :rebounds => 12,
              :assists => 12,
              :steals => 22,
              :blocks => 5,
              :slam_dunks => 12
            }
          }
        }
      }
    end
3
  • 1
    Can you show the content of game_hash hash? What's inside? It might help people to answer your question. Commented Jul 27, 2018 at 21:44
  • This question begs for an example. game_hash cannot be a hash as it is local to the method biggest_shoe. Do you mean game_hash to be a method (despite its name)? If it is to be a hash it needs to be passed to the method as an argument. Commented Jul 28, 2018 at 3:50
  • Your edit, 8 hours after you posted the question, renders the two answers that had already been posted nonsensical. That's a big no-no at SO. Downvote is mine. Commented Jul 28, 2018 at 20:13

3 Answers 3

2

Like a lot of common things, Ruby has a tool for that:

game_hash.values.map do |team_data|
  team_data[:players]
end.values.map do |stats|
  stats[:shoe]
end.max
Sign up to request clarification or add additional context in comments.

Comments

1
game_hash[:home][:players].max_by { |_name, stats| stats[:shoe] }.last[:shoe]
  #=> 19

Comments

0

This one liner to get all the shoe value into an array, then pick the max.

game_hash.map { |_, v| v[:players].map { |_, v| v[:shoe] } }.flatten.max

Creating a method to get the max for a chosen key

def pick_max_stat_from(h, key)
  h.map { |_, v| v[:players].map { |_, v| v[key] } }.flatten.max
end

pick_max_stat_from game_hash, :slam_dunks # => 15

Maybe this could be useful to get the max by location:

game_hash.transform_values { |v| v[:players].map { |_, v| v[:shoe] }.max }
# = {:home=>19, :away=>18}

3 Comments

You could skip the step of extracting the values by writing game_hash.map { |_,location|...
Got it. Thanks @CarySwoveland
These answers were all great resources! thank you so much for the info. I'm fairly new to ruby and all these different answers helped so much! Much appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.