Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement