Conversation
CalculatorWhat We're Looking For
Great job overall! This program works well, and I appreciate the way you used whitespace to break up your code. There were a few things that could be cleaned up that I've tried to call out below, but in general I am quite happy with this submission. Keep up the hard work! |
| begin | ||
| number_2 = gets.chomp | ||
| number_2 = Float(number_2) | ||
| rescue |
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?
| def divide_two_numbers(first_number, second_number) | ||
| if second_number == 0 | ||
| puts "#{first_number} / #{second_number} = According to Wikipedia, the result of any number divided by zero is undefined." | ||
| else |
There was a problem hiding this comment.
I like that you've included the division-specific error handling code in the division method. This feels like good organization to me.
| #create a method for each operator | ||
| def add_two_numbers(first_number, second_number) | ||
| puts "#{first_number} + #{second_number} = #{first_number + second_number}" | ||
| end |
There was a problem hiding this comment.
These methods all puts things. That ends up solving the problem, but in general you want to be in the habit of returning things from methods, and then having whoever called the method do the putsing.
| case operator | ||
| when "add", "+" | ||
| add_two_numbers(number_1, number_2) | ||
| when "subtract", "-" |
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).
Calculator
Congratulations! You're submitting your assignment.
Comprehension Questions