Skip to content

Instantly share code, notes, and snippets.

@royosherove
Created October 10, 2012 15:29
Show Gist options
  • Save royosherove/3866357 to your computer and use it in GitHub Desktop.
Save royosherove/3866357 to your computer and use it in GitHub Desktop.
start of string calculator kata in ruby
require "rspec"
class StringCalculator
def add(numbers)
return 0 if numbers == ""
return numbers.to_i unless numbers.include? ","
return numbers[0].to_i + numbers[2].to_i
end
def initialize
puts "initialized!"
end
end
describe StringCalculator do
describe ".add" do
context "empty input" do
it "should return the default zero" do
subject.add("").should == 0
end
end
context "single number" do
it "should return that number" do
subject.add("1").should == 1
end
specify { subject.add("2").should == 2 }
specify { subject.add("-2").should == -2 }
end
context "multiple numbers" do
it "should sum them up" do
subject.add("1,2").should == 3
end
specify { subject.add("1,3").should == 4}
specify { subject.add("1,2,3").should == 6}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment