Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

eighth step on ruby language

  • Loading branch information...
commit 26c5fe0161e962e07eddaa438ed588aaf2b12f4f 1 parent b3d0034
@jrdi jrdi authored
Showing with 14 additions and 4 deletions.
  1. +14 −4 jrdi/string_calculator.rb
View
18 jrdi/string_calculator.rb
@@ -2,15 +2,21 @@
class StringCalculator
- def self.add(string)
- delimiter = string.match(%r{^\/\/\[(.+)\]\\n}) && $1 || ','
+ def self.add(string)
- numbers = string.split(%r{[\\n, #{delimiter}]}).map(&:to_i).delete_if {|i| i > 1000}
+ delimiters = if delimiter = (string.match(%r{^\/\/\[(.+)\]\\n}) && $1)
+ delimiter.split('][')
+ else
+ [',']
+ end
+
+ numbers = string.split(%r{#{delimiters + ['\n']}}).map(&:to_i).delete_if {|i| i > 1000}
negatives = numbers.select {|number| number < 0}
raise "Negatives numbers not allowed: #{negatives.join(', ')}" if negatives.any?
- numbers.inject(0) {|total, number| total += number }
+ return 0 if numbers.empty?
+ numbers.reduce(:+)
end
end
@@ -51,4 +57,8 @@ def self.add(string)
it "should return 3 for '//[;;;]\n1;;;2'" do
StringCalculator.add('//[;;;]\n1;;;2').should == 3
end
+
+ it "should return 7 for '//[;;;][***]\n1;;;2***4'" do
+ StringCalculator.add('//[;;;][***]\n1;;;2***4').should == 7
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.