Skip to content

Instantly share code, notes, and snippets.

@tooky
Created July 29, 2014 09:57
Show Gist options
  • Save tooky/a75778f70499af2f9435 to your computer and use it in GitHub Desktop.
Save tooky/a75778f70499af2f9435 to your computer and use it in GitHub Desktop.
# Corey's challenge is to make the second test pass without ever making the first test fail.
# run ruby stack.rb to run the tests
# For more information see http://tooky.co.uk/kickstart-academy-podcast-with-corey-haines-and-sandi-metz/
class Stack
def empty?
true
end
def push(element)
@empty = false
end
end
def assert(expected, actual, msg="FAIL!")
if expected != actual
raise "Failed test. #{expected} should have been #{actual}"
end
end
# a new stack is empty
s = Stack.new
assert(true, s.empty?)
# a stack is not empty after I push
s = Stack.new
s.push 5
if false != s.empty?
raise "Stack should not have been empty"
end
@coreyhaines
Copy link

I forgot to mention one little constraint on this. You can ONLY ADD code on red and ONLY REMOVE code on green. And you must run your tests after each change, Katrina Owen-style. Otherwise, it is too straightforward.
My little rhyme that I use is

"Add code on red, remove code on green"

(Even though I'm told that doesn't rhyme)

One effect of this is that it is much harder to mutate the existing code, relying instead on adding and removing.

@coreyhaines
Copy link

I'd also start without lines 9-11 (the push method). Start with a very clean slate.

@kerryb
Copy link

kerryb commented Jul 29, 2014

Shouldn't the msg argument to #assert be used somewhere?

@coreyhaines
Copy link

HAHA! Yeah, that would be good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment