Conversation
CalculatorWhat We're Looking For
Great job overall! This program works well, and I like the way you've used methods and whitespace to break up your code. There are a few places that things could be cleaned up, which I've tried to call out below, but in general I am quite happy with this submission. Keep up the hard work. |
| until second_number.match?(/^(?=.*[\d])/) || | ||
| second_number.match?(/^(?=.*[-])(?=.*[\d])/) | ||
| puts "Hm, something went wrong..." | ||
| print "Please enter your second number: " |
There was a problem hiding this comment.
You've written almost exactly the same code here twice, to get the first number and the second number. Could you DRY that up by putting this logic in a method?
| case user_symbol | ||
| when "add", "+" | ||
| sum = add_numbers(first_number, second_number) | ||
| puts "#{first_number} + #{second_number} = #{sum}" |
There was a problem hiding this comment.
This code isn't repeated, but I think it would still increase readability to wrap this case/when section in a method. That would clearly delineate where it starts and ends, and make it explicit what data it needs to work. The method signature might be something like perform_calculation(op, first, second).
| when "divide", "/" | ||
| if second_number == 0 | ||
| puts "Undefined - cannot divide a number by zero" | ||
| else |
There was a problem hiding this comment.
Could you put this division-specific error handling code in the division method?
| # Addition method | ||
| def add_numbers(num1, num2) | ||
| return num1 + num2 | ||
| end |
There was a problem hiding this comment.
I like that you've broken each of these operations out as its own method, and that you're returning a value rather than putsing it here.
Calculator
Congratulations! You're submitting your assignment.
Comprehension Questions