Class: Player::Player

Inherits:
Object
  • Object
show all
Includes:
PlayerHelper
Defined in:
lib/baseball/player.rb

Constant Summary collapse

STATS =
%i[
  at_bats hits walks hbp sac_flies singles
  doubles triples hr put_outs assists errors
  er ip so stolen_bases caught_stealing
]

Instance Method Summary collapse

Methods included from PlayerHelper

#convert_third_of_inning, #figure_lead_and_trailing_zeroes, #figure_multiple_trailing_zeroes, #figure_trailing_zero, #remove_leading_zero, #third_of_an_inning_handler

Constructor Details

#initialize(hash) ⇒ Player

Returns a new instance of Player.



13
14
15
16
17
18
19
20
21
# 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

Instance Method Details

#babipObject



71
72
73
74
# 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_averageObject



23
24
25
26
# 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_nineObject



119
120
121
122
123
# 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

#eraObject



97
98
99
100
101
102
103
# 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_percentageObject



76
77
78
79
80
81
82
# 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

#isoObject



60
61
62
63
64
65
66
67
68
69
# 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_nineObject



113
114
115
116
117
# 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

#obpObject



28
29
30
31
32
33
34
# 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

#opsObject



43
44
45
46
47
48
49
50
51
52
# 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_createdObject



54
55
56
57
58
# 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

#slgObject



36
37
38
39
40
41
# 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_bbObject



125
126
127
128
129
# 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_percentageObject



84
85
86
87
88
# 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_runsObject



90
91
92
93
94
95
# 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

#whipObject



105
106
107
108
109
110
111
# 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