-
Notifications
You must be signed in to change notification settings - Fork 48
Lindsay - Calculator - Edges #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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