Skip to content

Instantly share code, notes, and snippets.

@adomokos
Last active August 29, 2015 14:12
Show Gist options
  • Save adomokos/a6e6c04c5209ce2c1274 to your computer and use it in GitHub Desktop.
Save adomokos/a6e6c04c5209ce2c1274 to your computer and use it in GitHub Desktop.
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