-
Notifications
You must be signed in to change notification settings - Fork 52
Branches - Michaela #35
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
Fantastic work on this project, Michaela! Your solutions are so well-organized, clever, logical, and readable. I really really really like the specific methods you pulled out for this assignment. I have a few comments and suggestions on how I might push this assignment for more refactoring if you had more time on it-- mostly, some ways to refactor those methods. However, overall this was a great project submission. Great work! |
|
||
# Check user-input operator for validity and store valid operator | ||
chosen_operator = gets.chomp.downcase | ||
chosen_operator = error_message_operators(chosen_operator, operators) |
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.
The error_message_operators
method is so so so clever. I like it a lot! Keep doing things like this! It may be interesting to rename this method to something like get_operator
, and refactor the code so that you only pass in operators
(instead of chosen_operator
and operators
) and also get the initial user operator in the method, too. For example, consider:
def get_operator(valid_input)
input = gets.chomp.downcase
until valid_input.keys.include? input.to_sym
puts "Oops! Your input was invalid. Your options are:"
print_operators
input = gets.chomp
end
return input
end
|
||
# Check user-input numbers for validity and store valid numbers | ||
chosen_number_1 = Float(gets) rescue nil | ||
chosen_number_1 = error_message_numbers(chosen_number_1) |
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.
Similar to my suggestion above, it may be interesting to refactor to a name like get_number
, and that method could change to
def error_message_numbers
input = Float(gets) rescue nil
while input == nil
puts "Oops! Your input was invalid. Please enter a number: "
input = Float(gets) rescue nil
end
return input
end
# Call the selected operator method with the user's selected number arguments | ||
# Store the resulting value as the answer | ||
# Note: operators are methods (send reads the given string as a method) | ||
answer = chosen_number_1.send(operators[:"#{chosen_operator}"], chosen_number_2) |
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 solution RULES! In practicality, I hope that you are never in a work situation where you will realistically need to use .send
, because that's a dangerous method! But for this specific case, it ends up being perfect. Well done, and nice work combining it with your operators
lookup hash.
puts "5. Modulo (%)" | ||
puts "6. Exponent (^)" | ||
print "\nPlease choose an operator (name or symbol): " | ||
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.
I love this method!!
Calculator
Congratulations! You're submitting your assignment.
Comprehension Questions