I've refactored the string calculator kata as much as I could and tried to maintain single responsibility. Is there anything I could factor differently? The specs can be reviewed here.
module StringCalculator
def self.add(string)
string.end_with?("\n") ? fail('ends in newline') : solve(string)
end
def self.solve(string)
verify(string)
custom = delimiter(string)
numerics = string.gsub(/\n/, custom).split(custom).map(&:to_i)
numerics.reject { |n| n > 1000 }.reduce(0, :+)
end
def self.delimiter(string)
string[0, 2] == '//' ? string[2, 1] : ','
end
def self.verify(string)
find = string.scan(/-\d+/)
fail("negatives not allowed: #{find.join(', ')}") if find.any?
end
private_class_method :verify, :delimiter, :solve
end
More importantly, I've thought of a case that involves the delimiter of a string that begins with //
to be a number (which is considered invalid). I've made a quick way of solving that issue like so. Any suggestions on that?
Invalid case:
def self.delimiter(string)
string[0, 2] == '//' ? check(string) || string[2, 1] : ','
end
def self.check(string)
fail("invalid delimiter: #{string[2, 1]}") if string[2, 1] =~ /[0-9]/
end