Skip to content

Instantly share code, notes, and snippets.

@mikesimons
Last active January 3, 2016 14:39
Show Gist options
  • Save mikesimons/8477282 to your computer and use it in GitHub Desktop.
Save mikesimons/8477282 to your computer and use it in GitHub Desktop.
String calculator kata
class StringCalculator
def calculate string = ""
string.split(/\D+/).map do |number|
number.to_i
end.reduce(&:+).to_i
end
end
require_relative 'calc'
describe StringCalculator do
before do
@calc = StringCalculator.new
end
it "should return zero with no arguments" do
@calc.calculate.should be 0
end
it "should return zero with empty string" do
@calc.calculate("").should be 0
end
it "should return zero with 0 as argument" do
@calc.calculate("0").should be 0
end
it "should return a bare number" do
@calc.calculate("1").should be 1
end
it "should return the sum of space separated numbers" do
@calc.calculate("2 3").should be 5
end
it "should return the sum of any whitespace separated numbers" do
@calc.calculate("2 4").should be 6
@calc.calculate("2\t4").should be 6
end
it "should accept a custom separator" do
@calc.calculate("//+1+2+3+4").should be 10
@calc.calculate("//a1a2a3a4").should be 10
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment