module BaseballCalc
Constants
- ERROR_COLOR
- HIGHLIGHT_COLOR
Colors to use with the colorize gem
- MostImprovedBatter
- PlayerAndValue
- TripleCrownResult
- VERSION
Public Class Methods
compute_and_write_slugging_percents(team, year)
click to toggle source
# File lib/baseball_calc.rb, line 59 def BaseballCalc.compute_and_write_slugging_percents(team, year) candidates = @players.values.select {|p| p.played_for?(team, year)} candidates.sort! {|a,b| a.full_name.upcase <=> b.full_name.upcase} color = BaseballCalc::HIGHLIGHT_COLOR $stdout.puts "" $stdout.puts "2. Slugging Percent for all #{candidates.size.to_s.colorize(color)} #{team.colorize(color)} players in #{year.colorize(color)}" candidates.each do |player| val = player.slugging_percent(year, team) $stdout.printf("%-30s", " #{player.full_name}") $stdout.printf("%-12s", player.player_id) $stdout.printf("%-20s", BaseballCalc.pretty_slugging_percent(val)) $stdout.print("\n") end end
compute_triple_crown_winner_in_year_for_league(year, league, min_at_bats)
click to toggle source
# File lib/baseball_calc.rb, line 100 def BaseballCalc.compute_triple_crown_winner_in_year_for_league(year, league, min_at_bats) candidates = BaseballCalc.players_with_min_at_bats(@players.values, min_at_bats, year) candidates = candidates.select {|player| player.played_in_league_during_year?(league, year)} result1 = BaseballCalc.player_and_max_value_for_method(:batting_avg_in_year_and_league, [year, league], candidates) result2 = BaseballCalc.player_and_max_value_for_method(:home_runs_in_year_and_league, [year, league], candidates) result3 = BaseballCalc.player_and_max_value_for_method(:rbis_in_year_and_league, [year, league], candidates) winning_player = (result1.player == result2.player) && (result1.player == result3.player) ? result1.player : nil BaseballCalc::TripleCrownResult.new(winning_player, year, league) end
compute_triple_crown_winners(years, min_at_bats)
click to toggle source
# File lib/baseball_calc.rb, line 82 def BaseballCalc.compute_triple_crown_winners(years, min_at_bats) results = [] years.each do |year| results << BaseballCalc.compute_triple_crown_winner_in_year_for_league(year, "AL", min_at_bats) results << BaseballCalc.compute_triple_crown_winner_in_year_for_league(year, "NL", min_at_bats) end results end
execute(player_file_path, batting_file_path)
click to toggle source
This is the main routine for starting computations and generating output. Any options should be determine in the bin file and passed into this method
# File lib/baseball_calc.rb, line 25 def BaseballCalc.execute(player_file_path, batting_file_path) @players = BaseballCalc::Importer.import(player_file_path, batting_file_path) BaseballCalc::Importer.write_errors(player_file_path, batting_file_path, 5) result = BaseballCalc.find_most_improved_batter("2009", "2010", 200) BaseballCalc.write_most_improved_batting_avg(result) BaseballCalc.compute_and_write_slugging_percents("OAK", "2007") results = BaseballCalc.compute_triple_crown_winners(["2011", "2012"], 400) # Passed as an array in case the years are ever pulled from the command line args BaseballCalc.write_triple_crown_winners(results) end
find_most_improved_batter(start_year, end_year, min_at_bats)
click to toggle source
# File lib/baseball_calc.rb, line 38 def BaseballCalc.find_most_improved_batter(start_year, end_year, min_at_bats) winner = BaseballCalc::MostImprovedBatter.new(nil, 0) candidates = BaseballCalc.players_with_min_at_bats(@players.values, min_at_bats, start_year) candidates = BaseballCalc.players_with_min_at_bats(candidates, min_at_bats, end_year) candidates.each do |player| val = player.batting_avg_improvement(start_year, end_year) winner = BaseballCalc::MostImprovedBatter.new(player, val, start_year, end_year) if val > winner.batting_avg_change end winner end
player_and_max_value_for_method(method_name, args, players)
click to toggle source
Iterates over the list of players and executes a method (with args) on each player. The return value of the dynamic method call is compared to the highest known value Each time a new high value is found, the PlayerAndValue
struct is updated with the new player and max value.
# File lib/baseball_calc.rb, line 120 def BaseballCalc.player_and_max_value_for_method(method_name, args, players) winner = BaseballCalc::PlayerAndValue.new(nil, 0) players.each do |player| total = player.send(method_name, *args) if total > winner.value winner.player = player winner.value = total end end winner end
players_with_min_at_bats(list, threshold, year)
click to toggle source
# File lib/baseball_calc.rb, line 112 def BaseballCalc.players_with_min_at_bats(list, threshold, year) list.select {|p| p.at_bats_at_least?(threshold, year)} end
pretty_slugging_percent(val)
click to toggle source
# File lib/baseball_calc.rb, line 74 def BaseballCalc.pretty_slugging_percent(val) return "No At Bats".colorize(BaseballCalc::HIGHLIGHT_COLOR) if val == -1 return "Missing Data".colorize(BaseballCalc::ERROR_COLOR) if val == -2 return "No At Bats"..colorize(BaseballCalc::HIGHLIGHT_COLOR) if val == -3 val.to_s.colorize(BaseballCalc::HIGHLIGHT_COLOR) end
write_most_improved_batting_avg(winner)
click to toggle source
# File lib/baseball_calc.rb, line 51 def BaseballCalc.write_most_improved_batting_avg(winner) color = BaseballCalc::HIGHLIGHT_COLOR $stdout.puts "" $stdout.print "1. The player with the most improved batting average from #{winner.start_year.colorize(color)} to" $stdout.print "#{winner.end_year.colorize(color)} is #{winner.player.full_name.colorize(color)} " $stdout.puts "[#{winner.player.player_id.colorize(color)}] with an improvement of #{winner.batting_avg_change.to_s.colorize(color)}." end
write_triple_crown_winners(results)
click to toggle source
# File lib/baseball_calc.rb, line 91 def BaseballCalc.write_triple_crown_winners(results) $stdout.puts "" $stdout.puts "3. Triple Crown Winners" results.each do |result| str = " #{result.league} #{result.year}" $stdout.puts result.player.nil? ? "#{str} - (No Winner)" : "#{str.colorize(BaseballCalc::HIGHLIGHT_COLOR)} - #{result.player.full_name.colorize(BaseballCalc::HIGHLIGHT_COLOR)}" end end