class BaseballCalc::BattingSummary

A BattingSummary represents a row of data in the Batting.csv file.

Attributes

at_bats[R]
hits[R]
home_runs[R]
league[R]
runs_batted_in[R]
team_id[R]
year_id[R]

Public Class Methods

new(player, data) click to toggle source
# File lib/baseball_calc/batting_summary.rb, line 8
def initialize(player, data)
        @player = player
        @year_id = data["yearID"]
        @league = data["league"].upcase
        @team_id = data["teamID"].upcase
        @games = data["G"].to_i
        @at_bats = data["AB"].to_i
        @hits = data["H"].to_i
        @runs = data["R"].to_i
        @doubles = data["2B"].to_i
        @triples = data["3B"].to_i
        @home_runs = data["HR"].to_i
        @runs_batted_in = data["RBI"].to_i
        @stolen_bases = data["SB"].to_i
        @caught_stealing = data["CS"].to_i
        @raw_data = data
end

Public Instance Methods

has_bad_batting_data?() click to toggle source

This should not check for an existing @player or @player_id

# File lib/baseball_calc/batting_summary.rb, line 28
def has_bad_batting_data?
        @year_id.blank? || @league.blank? || @team_id.blank? || @raw_data["G"].blank? || @raw_data["AB"].blank? ||
         @raw_data["H"].blank? || @raw_data["R"].blank? || @raw_data["2B"].blank? || @raw_data["3B"].blank? || @raw_data["HR"].blank? ||
                @raw_data["RBI"].blank? || @raw_data["SB"].blank? || @raw_data["CS"].blank?
end
slugging_percentage() click to toggle source

If the players batting data is spread across multiple Batting Summaries for a year, this method will not be accurate. However, if it is guaranteed that this Batting Summary has all of the Batting data for a year, then the answer will be correct. This is the case when computing slugging percentage for a specific team for a specific year.

# File lib/baseball_calc/batting_summary.rb, line 38
def slugging_percentage
        return -2 if has_bad_batting_data?
        return -1 if slugging_percentage_fields_are_all_zero?
        return -3 if @at_bats == 0 # Must be data in some fields, but there were no At Bats
        (((@hits - @doubles - @triples - @home_runs) + (2 * @doubles) + (3 * @triples) + (4 * @home_runs)) * 1.0 / @at_bats).round(4)
end

Private Instance Methods

slugging_percentage_fields_are_all_zero?() click to toggle source
# File lib/baseball_calc/batting_summary.rb, line 47
def slugging_percentage_fields_are_all_zero?
        @hits == 0 && @doubles == 0 && @triples == 0 && @home_runs == 0 && @at_bats == 0
end