Skip to content

Commit

Permalink
String calculator #16
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Jul 22, 2011
1 parent 4e0c7a6 commit 24f9886
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion string_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ def add(string)
string = string[4..-1]
end

negatives = []
result = 0
string.split(/#{delimiter}|\n/).each do |s|
raise ArgumentError, "no empty values allowed" if s.empty?
result = result + s.to_i
if s.to_i < 0
negatives << s
else
result = result + s.to_i
end
end
raise ArgumentError, "no negative numbers allowed: #{negatives.join(", ")}" unless negatives.empty?
result
end

Expand Down

0 comments on commit 24f9886

Please sign in to comment.