baseball 1.3.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eabf6dffbac13a71c6e482bfcf90a94637ae2dae29b2482fefc85b83643267b8
4
- data.tar.gz: 1492c0f74c510e2e0cdc8fa41006253ef2aed2d5b8cc809fed058775e6ca5979
3
+ metadata.gz: e73e7a5f818d6c42dd5f6ebd042eabe0e33b2dd3f827bde96878e1f420aac991
4
+ data.tar.gz: e8f34be1d4ab7bf48288759fe6444f72486e051d989b3a61296b197994fbeef2
5
5
  SHA512:
6
- metadata.gz: f6c6174eaec56a0a60ab9f99ffe4260af130eeb8eea7b5b428fb34f7f159d5144a8f22b611d9f6a7901084440e25dcbbe3e613f2fba2b155e10fdf8d43e0e1c0
7
- data.tar.gz: a2647dc3d2f617f075af88bdaddfc6a4c7f328084ca8b5b21b24238fc9247c9c6f12d386cf3f05baa2296555896f9551a4ff857bc1670f31d8211e0fc88071bf
6
+ metadata.gz: 03b7a0221fa93802db01c75a231de07c5aa03b282cb9e8aefcd5b47250c5c8bbf52029f8831ef5cf6b86cb14d73d1604ceca22634aa296e077fc55863507d6d4
7
+ data.tar.gz: 5240b6ec6bd4b5d7b4203730d43a01ea8e6222c205e3f75960f1039855f0d46b6fbc99716520998b4d68cb7c31477a94717d43e3bf7e0dceee6302640b13a2e8
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in baseball.gemspec
6
6
  gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ baseball (1.3.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ rake (10.5.0)
10
+
11
+ PLATFORMS
12
+ ruby
13
+
14
+ DEPENDENCIES
15
+ baseball!
16
+ bundler (~> 1.16)
17
+ rake (~> 10.0)
18
+
19
+ BUNDLED WITH
20
+ 1.16.0
data/README.md CHANGED
@@ -5,7 +5,7 @@ This Ruby gem contains a pre-created library for factoring baseball statistics.
5
5
  * Gem is still in development with more stats to be added.
6
6
 
7
7
  * Note: This project is in no way taking credit for creating any of the included baseball stats.
8
- This is a library created strictly to be used in easily figuring the included statistics based on the data received by the user.
8
+ This is a library created strictly to be used for easily figuring the included statistics based on the data received by the user.
9
9
 
10
10
  ## Installation
11
11
 
@@ -25,6 +25,8 @@ Or install it yourself as:
25
25
 
26
26
  ## Usage
27
27
 
28
+ ### Version 1 (1.3.0 and lower)
29
+
28
30
  All methods in `Baseball` take a hash as an argument. The hash should contain all the
29
31
  key-value pairs needed for the specific method called.
30
32
 
@@ -76,6 +78,42 @@ All values are returned as a string
76
78
  Libary includes pitching, fielding, running, and batting stats. See tests for full hash key-values needed.
77
79
  (More detailed documentation will be added shortly)
78
80
 
81
+ ### version 2 (Ruby Gem not yet released)
82
+
83
+ include a hash with all the key-value pairs desired for each individual statistic into `Baseball.compile()`
84
+
85
+ The result is an object with all statistics based off of the hash passed as the argument. You can then call each method in the object.
86
+
87
+ example:
88
+
89
+ ```ruby
90
+ your_player_hash = {
91
+ at_bats: 420,
92
+ hits: 134,
93
+ walks: 68,
94
+ hbp: 1,
95
+ sac_flies: 3,
96
+ singles: 77,
97
+ doubles: 27,
98
+ triples: 1,
99
+ hr: 29
100
+ }
101
+
102
+ player = Baseball.compile(your_player_hash)
103
+
104
+ player.batting_average # => ".319"
105
+ player.obp # => ".413"
106
+ player.ops # => "1.008"
107
+
108
+ ```
109
+
110
+ notes:
111
+
112
+ * each specific stats' required values must be passed, or you will not receive the correct result. For instance, if you try to get batting statistics, but only entered pitching values into your hash you will not receive correct results.
113
+
114
+ * since pitchers and batters share some stats (hits, walks) you will want to create two separate object records
115
+ (one for batting, one for pitching) for pitchers that bat.
116
+
79
117
  ## Development
80
118
 
81
119
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/Rakefile CHANGED
@@ -1,8 +1,8 @@
1
1
  require 'rake/testtask'
2
2
 
3
3
  Rake::TestTask.new do |t|
4
- t.libs << 'test'
4
+ t.libs << 'test'
5
5
  end
6
6
 
7
- desc "Run tests"
8
- task :default => :test
7
+ desc 'Run tests'
8
+ task default: :test
data/baseball.gemspec CHANGED
@@ -1,4 +1,3 @@
1
-
2
1
  lib = File.expand_path("../lib", __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require "baseball/version"
data/lib/baseball.rb CHANGED
@@ -1,106 +1,13 @@
1
1
  require 'baseball/version'
2
- require 'baseball/batting'
3
- require 'baseball/pitching'
4
- require 'baseball/fielding'
5
- require 'baseball/running'
6
2
  require 'baseball/player'
7
3
 
8
4
  module Baseball
9
-
10
- #table of contents
11
- #batting stats
12
- #pitching stats
13
- # fielding stats
14
- # running stats
15
-
5
+ include Player
16
6
  def self.version_number
17
7
  Baseball::VERSION
18
8
  end
19
9
 
20
- #batting stats
21
-
22
- def self.batting_average(player_hash)
23
- include Batting
24
- player = Batter.new(player_hash)
25
- player.batting_average
26
- end
27
-
28
- def self.obp(player_hash)
29
- include Batting
30
- player = Batter.new(player_hash)
31
- player.obp
32
- end
33
-
34
- def self.slg(player_hash)
35
- include Batting
36
- player = Batter.new(player_hash)
37
- player.slugging_percentage
38
- end
39
-
40
- def self.ops(player_hash)
41
- include Batting
42
- player = Batter.new(player_hash)
43
- player.ops
44
- end
45
-
46
- def self.runs_created(player_hash)
47
- include Batting
48
- player = Batter.new(player_hash)
49
- player.runs_created
50
- end
51
-
52
- #pitching stats
53
-
54
- def self.era(pitcher_hash)
55
- include Pitching
56
- pitcher = Pitcher.new(pitcher_hash)
57
- pitcher.era
58
- end
59
-
60
- def self.whip(pitcher_hash)
61
- include Pitching
62
- pitcher = Pitcher.new(pitcher_hash)
63
- pitcher.whip
10
+ def self.compile(arg)
11
+ Player.new(arg)
64
12
  end
65
-
66
- def self.k_per_nine(pitcher_hash)
67
- include Pitching
68
- pitcher = Pitcher.new(pitcher_hash)
69
- pitcher.k_per_nine
70
- end
71
-
72
- def self.bb_per_nine(pitcher_hash)
73
- include Pitching
74
- pitcher = Pitcher.new(pitcher_hash)
75
- pitcher.bb_per_nine
76
- end
77
-
78
- def self.so_per_bb(pitcher_hash)
79
- include Pitching
80
- pitcher = Pitcher.new(pitcher_hash)
81
- pitcher.so_per_bb
82
- end
83
-
84
- #fielding stats
85
-
86
- def self.fielding_percentage(fielder_hash)
87
- include Fielding
88
- fielder = Fielder.new(fielder_hash)
89
- fielder.fielding_percentage
90
- end
91
-
92
- # running stats
93
-
94
- def self.stolen_base_percentage(runner_hash)
95
- include Running
96
- runner = Runner.new(runner_hash)
97
- runner.stolen_base_percentage
98
- end
99
-
100
- def self.stolen_base_runs(runner_hash)
101
- include Running
102
- runner = Runner.new(runner_hash)
103
- runner.stolen_base_runs
104
- end
105
-
106
13
  end
@@ -1,50 +1,126 @@
1
1
  module Player
2
+ require 'baseball/player_helper'
3
+ class Player
4
+ include PlayerHelper
5
+ STATS = %i[
6
+ at_bats hits walks hbp sac_flies singles
7
+ doubles triples hr put_outs assists errors
8
+ er ip so stolen_bases caught_stealing
9
+ ]
2
10
 
3
- class PlayerTemplate
4
- attr_accessor :player
5
- def initialize(player)
6
- @player = player
11
+ attr_accessor(*STATS)
12
+
13
+ def initialize(hash)
14
+ STATS.each do |iv|
15
+ if iv != :singles
16
+ instance_variable_set("@#{iv}", hash.fetch(iv, 0))
17
+ end
18
+ end
19
+ # figures and sets default value of singles if not included by user
20
+ @singles ||= @hits - (@doubles + @triples + @hr)
7
21
  end
8
22
 
9
- def figure_lead_and_trailing_zeroes(arg)
10
- revised_number = self.remove_leading_zero(arg)
23
+ def batting_average
24
+ avg = (hits.to_f / at_bats.to_f).round(3)
25
+ figure_lead_and_trailing_zeroes(avg)
26
+ end
27
+
28
+ def obp
29
+ times_on_base = hits.to_f + walks.to_f + hbp.to_f
30
+ times_at_plate = at_bats.to_f + walks.to_f + hbp.to_f + sac_flies.to_f
31
+ on_base_percentage = times_on_base.to_f / times_at_plate.to_f
32
+ player_obp = on_base_percentage.round(3)
33
+ figure_lead_and_trailing_zeroes(player_obp)
34
+ end
11
35
 
12
- if revised_number.length === 3
13
- revised_number = "#{revised_number}0"
14
- elsif revised_number.length === 2
15
- revised_number = "#{revised_number}00"
36
+ def slg
37
+ total_bases = singles + (doubles * 2) + (triples * 3) + hr * 4
38
+ slg_pct = total_bases.to_f / at_bats.to_f
39
+ slg_pct = slg_pct.round(3)
40
+ figure_lead_and_trailing_zeroes(slg_pct)
41
+ end
42
+
43
+ def ops
44
+ player_ops = obp.to_f + slg.to_f
45
+ player_ops_string = player_ops.round(3)
46
+ player_ops_string = player_ops_string.to_s
47
+ if player_ops_string[0] == "0"
48
+ remove_leading_zero(player_ops_string)
16
49
  else
17
- return revised_number
50
+ player_ops_string
18
51
  end
19
52
  end
20
53
 
21
- def third_of_an_inning_handler(innings)
22
- innings_string = innings.to_s
23
- final_fig = innings_string[0..(innings_string.length - 2)]
24
- final_num = innings_string[(innings_string.length - 2)..innings_string.length].to_f
25
- returnable_innings = final_fig.to_f
26
- if final_num == 0.1
27
- returnable_innings += 0.33
28
- returnable_innings.to_s
29
- elsif final_num == 0.2
30
- returnable_innings += 0.66
31
- returnable_innings.to_s
54
+ def runs_created
55
+ total_production = (singles + (doubles * 2) + (triples * 3) + hr * 4) * (hits + walks)
56
+ runs_created_figure = total_production.to_f / (at_bats + walks).to_f
57
+ runs_created_figure.round(2).to_s
58
+ end
59
+
60
+ def iso
61
+ isolated_power = slg.to_f - batting_average.to_f
62
+ isolated_power = figure_lead_and_trailing_zeroes(isolated_power.round(3))
63
+ # check whole number is not 0, if so, figure extra zeroes
64
+ if isolated_power[0] > "0"
65
+ figure_multiple_trailing_zeroes(isolated_power)
32
66
  else
33
- innings
67
+ isolated_power
34
68
  end
35
69
  end
36
70
 
37
- def remove_leading_zero(arg)
38
- arg.to_s.sub("0", "")
71
+ def fielding_percentage
72
+ plays = put_outs + assists
73
+ plays_plus_errors = plays + errors
74
+ player_avg = plays.to_f / plays_plus_errors.to_f
75
+ avg = player_avg.round(3)
76
+ figure_lead_and_trailing_zeroes(avg)
39
77
  end
40
78
 
41
- def figure_trailing_zero(arg)
42
- sprintf "%.2f", arg
79
+ def stolen_base_percentage
80
+ player_stolen_base_percentage = stolen_bases.to_f / (stolen_bases.to_f + caught_stealing.to_f)
81
+ stealing_average = player_stolen_base_percentage.round(3)
82
+ figure_lead_and_trailing_zeroes(stealing_average)
43
83
  end
44
84
 
45
- def figure_multiple_trailing_zeroes(arg)
46
- sprintf "%.3f", arg
85
+ def stolen_base_runs
86
+ stolen_base_adjustment = stolen_bases.to_f * 0.3
87
+ caught_stealing_adjustment = caught_stealing.to_f * 0.6
88
+ adjusted_stolen_base_runs = stolen_base_adjustment - caught_stealing_adjustment
89
+ adjusted_stolen_base_runs.round(3).to_s
47
90
  end
48
91
 
92
+ def era
93
+ earned_runs = er * 9
94
+ innings = third_of_an_inning_handler(ip)
95
+ avg = earned_runs / innings.to_f
96
+ avg.round(2)
97
+ figure_trailing_zero(avg)
98
+ end
99
+
100
+ def whip
101
+ corrected_innings = third_of_an_inning_handler(ip).to_f
102
+ walks_plus_hits = walks + hits
103
+ figured_whip = walks_plus_hits / corrected_innings
104
+ figured_whip.round(3).to_s
105
+ figure_multiple_trailing_zeroes(figured_whip)
106
+ end
107
+
108
+ def k_per_nine
109
+ full_games = ip / 9
110
+ so9 = so / full_games.to_f
111
+ so9.round(1).to_s
112
+ end
113
+
114
+ def bb_per_nine
115
+ full_games = ip / 9
116
+ walks_per_nine = walks / full_games.to_f
117
+ walks_per_nine.round(1).to_s
118
+ end
119
+
120
+ def so_per_bb
121
+ ratio = so.to_f / walks.to_f
122
+ ratio.round(2)
123
+ figure_trailing_zero(ratio)
124
+ end
49
125
  end
50
126
  end
@@ -0,0 +1,49 @@
1
+ module PlayerHelper
2
+ # adds additional zeroes to follow baseball stats. ex: .300 vs. .3
3
+ def figure_lead_and_trailing_zeroes(arg)
4
+ revised_number = remove_leading_zero(arg)
5
+
6
+ if revised_number.length == 3
7
+ "#{revised_number}0"
8
+ elsif revised_number.length == 2
9
+ "#{revised_number}00"
10
+ else
11
+ revised_number
12
+ end
13
+ end
14
+
15
+ # cuts ERA into whole number, decimal for format manipulation
16
+ def third_of_an_inning_handler(innings)
17
+ innings_string = innings.to_s
18
+ whole_innings = innings_string[0..(innings_string.length - 2)]
19
+ decimal_num = innings_string[(innings_string.length - 2)..innings_string.length].to_f
20
+ whole_innings_string = whole_innings.to_f
21
+ convert_third_of_inning(decimal_num, whole_innings_string, innings)
22
+ end
23
+
24
+ # converts .0, .1, and .2 innings entered to actual third of innings
25
+ def convert_third_of_inning(decimal, whole_num, original)
26
+ if decimal == 0.1
27
+ whole_num += 0.33
28
+ whole_num.to_s
29
+ elsif decimal == 0.2
30
+ whole_num += 0.66
31
+ whole_num.to_s
32
+ else
33
+ original
34
+ end
35
+ end
36
+
37
+ # removes whole number 0 to follow baseball stats standards. Ex: .300
38
+ def remove_leading_zero(arg)
39
+ arg.to_s.sub("0", "")
40
+ end
41
+
42
+ def figure_trailing_zero(arg)
43
+ format "%.2f", arg
44
+ end
45
+
46
+ def figure_multiple_trailing_zeroes(arg)
47
+ format "%.3f", arg
48
+ end
49
+ end
@@ -1,3 +1,3 @@
1
1
  module Baseball
2
- VERSION = "1.3.0"
2
+ VERSION = "2.0.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: baseball
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brent Busby
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-03 00:00:00.000000000 Z
11
+ date: 2018-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -48,6 +48,7 @@ files:
48
48
  - ".gitignore"
49
49
  - CODE_OF_CONDUCT.md
50
50
  - Gemfile
51
+ - Gemfile.lock
51
52
  - LICENSE.txt
52
53
  - README.md
53
54
  - Rakefile
@@ -55,11 +56,8 @@ files:
55
56
  - bin/console
56
57
  - bin/setup
57
58
  - lib/baseball.rb
58
- - lib/baseball/batting.rb
59
- - lib/baseball/fielding.rb
60
- - lib/baseball/pitching.rb
61
59
  - lib/baseball/player.rb
62
- - lib/baseball/running.rb
60
+ - lib/baseball/player_helper.rb
63
61
  - lib/baseball/version.rb
64
62
  homepage: https://github.com/buzzamus/baseball-gem
65
63
  licenses:
@@ -81,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
79
  version: '0'
82
80
  requirements: []
83
81
  rubyforge_project:
84
- rubygems_version: 2.7.2
82
+ rubygems_version: 2.7.6
85
83
  signing_key:
86
84
  specification_version: 4
87
85
  summary: a Ruby Gem library for calculating baseball statistics
@@ -1,48 +0,0 @@
1
- require 'baseball/player'
2
-
3
- module Batting
4
- class Batter < Player::PlayerTemplate
5
-
6
- def batting_average
7
- avg = @player[:hits].to_f / @player[:at_bats].to_f
8
- player_average = avg.round(3)
9
- figure_lead_and_trailing_zeroes(player_average)
10
- end
11
-
12
- def obp
13
- times_on_base = @player[:hits].to_f + @player[:walks].to_f + @player[:hbp].to_f
14
- times_at_plate = @player[:at_bats].to_f + @player[:walks].to_f + @player[:hbp].to_f + @player[:sac_flies]
15
- obp = times_on_base.to_f / times_at_plate.to_f
16
- player_obp = obp.round(3)
17
- figure_lead_and_trailing_zeroes(player_obp)
18
- end
19
-
20
- def slugging_percentage
21
- total_bases = @player[:singles] + (@player[:doubles] * 2) + (@player[:triples] * 3) + @player[:hr] * 4
22
- slg = total_bases.to_f / @player[:at_bats].to_f
23
- slg = slg.round(3)
24
- figure_lead_and_trailing_zeroes(slg)
25
- end
26
-
27
- def ops
28
- player_ops = self.obp.to_f + self.slugging_percentage.to_f
29
- player_ops_string = player_ops.round(3)
30
- player_ops_string = player_ops_string.to_s
31
- if player_ops_string[0] === "0"
32
- remove_leading_zero(player_ops_string)
33
- else
34
- player_ops_string
35
- end
36
- end
37
-
38
- def runs_created
39
- opportunities = @player[:at_bats] + @player[:walks]
40
- times_on_base = @player[:hits] + @player[:walks]
41
- total_bases = @player[:singles] + (@player[:doubles] * 2) + (@player[:triples] * 3) + @player[:hr] * 4
42
- total_production = total_bases * times_on_base
43
- runs_created_figure = total_production.to_f / opportunities.to_f
44
- runs_created_figure.round(2).to_s
45
- end
46
- end
47
-
48
- end
@@ -1,15 +0,0 @@
1
- require 'baseball/player'
2
-
3
- module Fielding
4
- class Fielder < Player::PlayerTemplate
5
-
6
- def fielding_percentage
7
- plays = @player[:put_outs] + @player[:assists]
8
- plays_plus_errors = plays + @player[:errors]
9
- player_avg = plays.to_f / plays_plus_errors.to_f
10
- avg = player_avg.round(3)
11
- figure_lead_and_trailing_zeroes(avg)
12
- end
13
- end
14
-
15
- end
@@ -1,41 +0,0 @@
1
- require 'baseball/player'
2
-
3
- module Pitching
4
- class Pitcher < Player::PlayerTemplate
5
-
6
- def era
7
- earned_runs = @player[:er] * 9
8
- avg = earned_runs / @player[:ip].to_f
9
- avg.round(2)
10
- figure_trailing_zero(avg)
11
- end
12
-
13
- def whip
14
- corrected_innings = third_of_an_inning_handler(@player[:ip]).to_f
15
- walks_plus_hits = @player[:walks] + @player[:hits]
16
- figured_whip = walks_plus_hits / corrected_innings
17
- figured_whip.round(3).to_s
18
- figure_multiple_trailing_zeroes(figured_whip)
19
- end
20
-
21
- def k_per_nine
22
- full_games = @player[:ip] / 9
23
- so9 = @player[:so] / full_games.to_f
24
- so9.round(1).to_s
25
- end
26
-
27
- def bb_per_nine
28
- full_games = @player[:ip] / 9
29
- walks_per_nine = @player[:walks] / full_games.to_f
30
- walks_per_nine.round(1).to_s
31
- end
32
-
33
- def so_per_bb
34
- ratio = @player[:so].to_f / @player[:walks].to_f
35
- ratio.round(2)
36
- figure_trailing_zero(ratio)
37
- end
38
-
39
- end
40
-
41
- end
@@ -1,20 +0,0 @@
1
- require 'baseball/player'
2
-
3
- module Running
4
- class Runner < Player::PlayerTemplate
5
-
6
- def stolen_base_percentage
7
- player_stolen_base_percentage = @player[:stolen_bases].to_f / (@player[:stolen_bases ].to_f + @player[:caught_stealing].to_f)
8
- stealing_average = player_stolen_base_percentage.round(3)
9
- figure_lead_and_trailing_zeroes(stealing_average)
10
- end
11
-
12
- def stolen_base_runs
13
- stolen_base_adjustment = @player[:stolen_bases].to_f * 0.3
14
- caught_stealing_adjustment = @player[:caught_stealing].to_f * 0.6
15
- adjusted_stolen_base_runs = stolen_base_adjustment - caught_stealing_adjustment
16
- adjusted_stolen_base_runs.round(3).to_s
17
- end
18
- end
19
-
20
- end