-
-
Save rab/1815ac870e0aec4ff24990a4b6ce6f90 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
def get_input(prompt = '--> ') | |
puts '' | |
puts 'Please enter a number, odd or even.' | |
print prompt | |
# if the end-of-input was reached and gets returned a nil | |
# or the word 'exit' or 'quit' appears (ignoring case) in the input | |
if (input = gets).nil? || /exit|quit/i =~ input | |
nil # just return a nil | |
else | |
input.to_i # otherwise, return the integer conversion | |
end | |
end | |
def test_user_input(number) | |
"'#{number}' is an #{number.even? ? 'EVEN' : 'odd'} number.\n" | |
end | |
while (number = get_input) # while the number I capture from get_input isn't nil (or false) | |
puts test_user_input(number) | |
end |
RegExp#match? doesn't exist in ruby 2.2.2 so use RegExp#=~ instead.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that this is still not quite perfect as you can see by: