Skip to content

Instantly share code, notes, and snippets.

@Jaltman429
Created June 11, 2013 03:47
Show Gist options
  • Save Jaltman429/5754369 to your computer and use it in GitHub Desktop.
Save Jaltman429/5754369 to your computer and use it in GitHub Desktop.
hashketball
# Hashketball Nests
#
# Great news! You're going to an NBA game! The only catch is that you've been
# volunteered to keep stats at the game.
#
# Using Nested Hashes, define a game, with two teams, their players, and the players stats:
#
# The game has two teams.
#
# A team has:
# - A name
# - Two colors
#
# Each team should have at least 5 players
#
# Each player should have a:
# - name
# - number (like their jersey number)
# - shoe size
#
# Each player should have the following stats:
# - points
# - rebounds
# - assists
# - steals
# - blocks
# - slam dunks
game = {
:team1 => {
:name => 'bulls',
:colors => ['red', 'white'],
:players => {
:player1 => {
:name => "john",
:number => 34,
:shoesize => 12,
:stats => {
:points => 42,
:rebounds => 34,
:assists => 21,
:steals => 22,
:blocks => 53,
:slamdunks => 3
}
},
:player2 => {
:name => "bob",
:number => 54,
:shoesize => 16,
:stats => {
:points => 42,
:rebounds => 34,
:assists => 21,
:steals => 22,
:blocks => 53,
:slamdunks => 3
}
},
:player3 => {
:name => "steve",
:number => 12,
:shoesize => 10,
:stats => {
:points => 42,
:rebounds => 34,
:assists => 21,
:steals => 22,
:blocks => 53,
:slamdunks => 3
}
},
:player4 => {
:name => "neil",
:number => 52,
:shoesize => 8,
:stats => {
:points => 42,
:rebounds => 34,
:assists => 21,
:steals => 22,
:blocks => 53,
:slamdunks => 3
}
},
:player5 => {
:name => "aaron",
:number => 23,
:shoesize => 11,
:stats => {
:points => 42,
:rebounds => 34,
:assists => 21,
:steals => 22,
:blocks => 53,
:slamdunks => 3
}
},
}
},
:team2 => {
:name => 'spurs',
:colors => ['black', 'white'],
:players => {
:player1 => {
:name => "john",
:number => 34,
:shoesize => 12,
:stats => {
:points => 42,
:rebounds => 34,
:assists => 21,
:steals => 22,
:blocks => 53,
:slamdunks => 3
}
},
:player2 => {
:name => "bob",
:number => 54,
:shoesize => 16,
:stats => {
:points => 42,
:rebounds => 34,
:assists => 21,
:steals => 22,
:blocks => 53,
:slamdunks => 3
}
},
:player3 => {
:name => "steve",
:number => 12,
:shoesize => 10,
:stats => {
:points => 42,
:rebounds => 34,
:assists => 21,
:steals => 22,
:blocks => 53,
:slamdunks => 3
}
},
:player4 => {
:name => "neil",
:number => 52,
:shoesize => 8,
:stats => {
:points => 42,
:rebounds => 34,
:assists => 21,
:steals => 22,
:blocks => 53,
:slamdunks => 3
}
},
:player5 => {
:name => "aaron",
:number => 23,
:shoesize => 11,
:stats => {
:points => 42,
:rebounds => 34,
:assists => 21,
:steals => 22,
:blocks => 53,
:slamdunks => 3
}
},
}
}
}
# Using the power of Ruby, and the Hashes you created above, answer the following questions:
# Return the number of points scored for any player:
#
def points_scored (game, team, player)
points = game[team][:players][player][:stats][:points]
end
puts points_scored(game, :team1, :player1)
# Return the shoe size for any player:
#
def shoe_size (game, team, player)
shoe = game[team][:players][player][:shoesize]
end
puts shoe_size(game, :team1, :player1)
# Return both colors for any team:
#
def colors (game, team)
color = game[team][:colors]
end
puts colors(game, :team1)
# Return both teams names:
#
def name (game, team)
name = game[team][:colors]
end
puts name(game, :team1)
# Return all the player numbers for a team:
#
def player_numbers (game, team)
number1 = game[team][:players][:player1][:number].to_s
number2 = game[team][:players][:player2][:number].to_s
number3 = game[team][:players][:player3][:number].to_s
number4 = game[team][:players][:player4][:number].to_s
number5 = game[team][:players][:player5][:number].to_s
return number1 + ", " + number2 + ", " + number3 + ", " + number4 + ", " + number5 + ", "
end
puts player_numbers(game, :team1)
# Return all the stats for a player:
#
def stats (game, team, player)
stats = game[team][:players][player][:stats]
end
puts stats(game, :team1, :player)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment