class Player::Player

Constants

STATS

Public Class Methods

new(hash) click to toggle source
# File lib/baseball/player.rb, line 13
def initialize(hash)
  STATS.each do |iv|
    if iv != :singles
      instance_variable_set("@#{iv}", hash.fetch(iv, 0))
    end
  end
  # figures and sets default value of singles if not included by user
  @singles ||= @hits - (@doubles + @triples + @hr)
end

Public Instance Methods

babip() click to toggle source
# File lib/baseball/player.rb, line 71
def babip
  initial_babip = (hits - hr).to_f / (at_bats - so - hr + sac_flies).to_f
  figure_lead_and_trailing_zeroes(initial_babip.round(3))
end
batting_average() click to toggle source
# File lib/baseball/player.rb, line 23
def batting_average
  avg = (hits.to_f / at_bats.to_f).round(3)
  figure_lead_and_trailing_zeroes(avg)
end
bb_per_nine() click to toggle source
# File lib/baseball/player.rb, line 119
def bb_per_nine
  full_games = ip / 9
  walks_per_nine = walks / full_games.to_f
  walks_per_nine.round(1).to_s
end
era() click to toggle source
# File lib/baseball/player.rb, line 97
def era
  earned_runs = er * 9
  innings = third_of_an_inning_handler(ip)
  avg = earned_runs / innings.to_f
  avg.round(2)
  figure_trailing_zero(avg)
end
fielding_percentage() click to toggle source
# File lib/baseball/player.rb, line 76
def fielding_percentage
  plays = put_outs + assists
  plays_plus_errors = plays + errors
  player_avg = plays.to_f / plays_plus_errors.to_f
  avg = player_avg.round(3)
  figure_lead_and_trailing_zeroes(avg)
end
iso() click to toggle source
# File lib/baseball/player.rb, line 60
def iso
  isolated_power = slg.to_f - batting_average.to_f
  isolated_power = figure_lead_and_trailing_zeroes(isolated_power.round(3))
  # check whole number is not 0, if so, figure extra zeroes
  if isolated_power[0] > "0"
    figure_multiple_trailing_zeroes(isolated_power)
  else
    isolated_power
  end
end
k_per_nine() click to toggle source
# File lib/baseball/player.rb, line 113
def k_per_nine
  full_games = ip / 9
  so9 = so / full_games.to_f
  so9.round(1).to_s
end
obp() click to toggle source
# File lib/baseball/player.rb, line 28
def obp
  times_on_base = hits.to_f + walks.to_f + hbp.to_f
  times_at_plate = at_bats.to_f + walks.to_f + hbp.to_f + sac_flies.to_f
  on_base_percentage = times_on_base.to_f / times_at_plate.to_f
  player_obp = on_base_percentage.round(3)
  figure_lead_and_trailing_zeroes(player_obp)
end
ops() click to toggle source
# File lib/baseball/player.rb, line 43
def ops
  player_ops = obp.to_f + slg.to_f
  player_ops_string = player_ops.round(3)
  player_ops_string = player_ops_string.to_s
  if player_ops_string[0] == "0"
    figure_lead_and_trailing_zeroes(player_ops_string)
  else
    figure_multiple_trailing_zeroes(player_ops_string)
  end
end
runs_created() click to toggle source
# File lib/baseball/player.rb, line 54
def runs_created
  total_production = (singles + (doubles * 2) + (triples * 3) + hr * 4) * (hits + walks)
  runs_created_figure = total_production.to_f / (at_bats + walks).to_f
  runs_created_figure.round(2).to_s
end
slg() click to toggle source
# File lib/baseball/player.rb, line 36
def slg
  total_bases = singles + (doubles * 2) + (triples * 3) + hr * 4
  slg_pct = total_bases.to_f / at_bats.to_f
  slg_pct = slg_pct.round(3)
  figure_lead_and_trailing_zeroes(slg_pct)
end
so_per_bb() click to toggle source
# File lib/baseball/player.rb, line 125
def so_per_bb
  ratio = so.to_f / walks.to_f
  ratio.round(2)
  figure_trailing_zero(ratio)
end
stolen_base_percentage() click to toggle source
# File lib/baseball/player.rb, line 84
def stolen_base_percentage
  player_stolen_base_percentage = stolen_bases.to_f / (stolen_bases.to_f + caught_stealing.to_f)
  stealing_average = player_stolen_base_percentage.round(3)
  figure_lead_and_trailing_zeroes(stealing_average)
end
stolen_base_runs() click to toggle source
# File lib/baseball/player.rb, line 90
def stolen_base_runs
  stolen_base_adjustment = stolen_bases.to_f * 0.3
  caught_stealing_adjustment = caught_stealing.to_f * 0.6
  adjusted_stolen_base_runs = stolen_base_adjustment - caught_stealing_adjustment
  adjusted_stolen_base_runs.round(3).to_s
end
whip() click to toggle source
# File lib/baseball/player.rb, line 105
def whip
  corrected_innings = third_of_an_inning_handler(ip).to_f
  walks_plus_hits = walks + hits
  figured_whip = walks_plus_hits / corrected_innings
  figured_whip.round(3).to_s
  figure_multiple_trailing_zeroes(figured_whip)
end