Skip to content

Instantly share code, notes, and snippets.

@sbaus42
Created May 10, 2018 13:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbaus42/20c8b0e514319868e9a04bd02e85237e to your computer and use it in GitHub Desktop.
Save sbaus42/20c8b0e514319868e9a04bd02e85237e to your computer and use it in GitHub Desktop.
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