Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
String Calculator Kata in Ruby - using enumerable methods
# Solving the String Calculator Kata
# http://osherove.com/tdd-kata-1/
class StringCalculator
def self.add(numbers)
return 0 if numbers.empty?
numbers.split(",").map(&:to_i).reduce(:+)
#splitted_array = numbers.split(",")
#splitted_array.map! do |element|
#element.to_i
#end
#splitted_array.inject(0) do |sum, element|
#sum += element
#end
end
end
RSpec.describe StringCalculator do
it("returns 0 for empty string") do
result = StringCalculator.add("")
expect(result).to eq(0)
end
it("returns 1 for '1'") do
result = StringCalculator.add("1")
expect(result).to eq(1)
end
it("returns 3 for '1,2'") do
result = StringCalculator.add("1,2")
expect(result).to eq(3)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.