Skip to content

Instantly share code, notes, and snippets.

@Pcushing
Created June 18, 2012 22:12
Show Gist options
  • Save Pcushing/2951060 to your computer and use it in GitHub Desktop.
Save Pcushing/2951060 to your computer and use it in GitHub Desktop.
Baseball team exercise w/ rspec
require 'rspec'
class BaseballTeam
def initialize
@starters = { 1 => { 'Aviles' => 'SS' }, 2 => { 'Pedroia' => '2B' }, 3 => { 'Youkilis' => '1B' },
4 => { 'Ortiz' => 'DH' }, 5 => { 'Middlebrooks' => '3B' }, 6 => { 'Gonzalez' => 'OF' },
7 => { 'Saltalamacchia' => 'C' }, 8 => { 'McDonald' => 'OF' }, 9 => { 'Byrd' => 'OF' },
0 => { 'Lester' => 'P' } }
end
def get_lineup
@starters
end
def batting_order
ordered_lineup = []
@starters.each do |k,v|
(ordered_lineup) << (@starters[k].keys[0]) if (@starters[k].values[0] != "P")
end
ordered_lineup
end
def substitution(lineup_number, bench_player)
@starting_player = @starters[lineup_number]
@starting_player = bench_player if @starting_player.values[0] == bench_player.values[0]
@starters[lineup_number] = @starting_player
end
end
#################################################################
describe BaseballTeam do
before do
@red_sox = BaseballTeam.new
end
it "should verify you have your lineup hash table" do
@red_sox.get_lineup.should eq(
{ 1 => { 'Aviles' => 'SS' }, 2 => { 'Pedroia' => '2B' }, 3 => { 'Youkilis' => '1B' },
4 => { 'Ortiz' => 'DH' }, 5 => { 'Middlebrooks' => '3B' }, 6 => { 'Gonzalez' => 'OF' },
7 => { 'Saltalamacchia' => 'C' }, 8 => { 'McDonald' => 'OF' }, 9 => { 'Byrd' => 'OF' },
0 => { 'Lester' => 'P' } })
end
it "should print the batting order without the pitcher" do
@red_sox.batting_order.should eq(%w(Aviles Pedroia Youkilis Ortiz
Middlebrooks Gonzalez Saltalamacchia McDonald
Byrd))
end
it "should replace a P for a P" do
@red_sox.substitution(0, { 'Aceves' => 'P' })
@red_sox.get_lineup[0].should eq({ 'Aceves' => 'P' })
end
it "should replace an OF with an OF" do
@red_sox.substitution(8, { 'Podsednik' => 'OF' })
@red_sox.get_lineup[8].should eq({ 'Podsednik' => 'OF' })
end
it "should fail if the positions don't match" do
@red_sox.substitution(0, { 'Podsednik' => 'OF' })
@red_sox.get_lineup[0].should eq({ 'Lester' => 'P' })
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment