Skip to content

Instantly share code, notes, and snippets.

@Pcushing
Created June 18, 2012 21:02
Show Gist options
  • Save Pcushing/2950678 to your computer and use it in GitHub Desktop.
Save Pcushing/2950678 to your computer and use it in GitHub Desktop.
Delete soon
# Create a Red Sox class that represents the starting line-up of the Red Sox in a recent game. The line-up at the start of the game, 1 through 9, is given below with the pitcher indicated at 0 as he doesn't bat (sorry, NL fans):
#
# * 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
#
# Print the lineup in order, starting with 0 and ending at 9.
#
# It should accept a new player into the lineup as long as they play the same position. Here's a few guys you might want to sub in during the game Crawford (OF), Bard (P), Shoppach (C), Aceves, (P), Punto (2B), Podsednik (OF).
require 'rspec'
class BaseballTeam
def initialize
@lineup = { 1 => { 'Aviles' => 'SS' }, 2 => { 'Pedroia' => '2B' }, 3 => { 'Youkilis' => '3B' },
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_hash
@lineup
end
def lineup_order
ordered_lineup = []
(0..9).each do |i|
ordered_lineup << @lineup[i].keys*","
end
ordered_lineup
end
def substitution(replace_number, replacement_hash)
@lineup[replace_number] = replacement_hash
lineup_order
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_hash.should eq(
{ 1 => { 'Aviles' => 'SS' }, 2 => { 'Pedroia' => '2B' }, 3 => { 'Youkilis' => '3B' },
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 lineup names in order" do
@red_sox.lineup_order.should eq(%w(Lester 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' }).should eq(%w(Aceves Aviles Pedroia Youkilis Ortiz
Middlebrooks Gonzalez Saltalamacchia McDonald
Byrd))
end
it "should replace an OF with an OF" do
@red_sox.substitution(8, { 'Podsednik' => 'OF' }).should eq(%w(Lester Aviles Pedroia Youkilis Ortiz
Middlebrooks Gonzalez Saltalamacchia Podsednik
Byrd))
end
it "should fail if the positions don't match" do
@red_sox.substitution(0, { 'Podsednik' => 'OF' }).should eq(%w(Lester Aviles Pedroia Youkilis Ortiz
Middlebrooks Gonzalez Saltalamacchia McDonald
Byrd))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment