Created
May 10, 2018 13:39
-
-
Save sbaus42/20c8b0e514319868e9a04bd02e85237e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module StringCalculator | |
class << self | |
def add string_numbers = nil | |
return 0 unless string_numbers | |
delimeters = get_delimeters string_numbers | |
num_array = string_numbers.split delimeters | |
raise 'negative numbers not allowed' if num_array.join().match(/-[0-9]/) | |
num_array.select{|i| i.to_i < 1000} | |
.map(&:to_i) | |
.reduce(&:+) | |
end | |
def get_delimeters string | |
user_delimeter = unless string.chars.first.match(/\d/) | |
"|" + string.chars.first | |
else | |
"" | |
end | |
/\n|\,#{user_delimeter}/ | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment