Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. # frozen_string_literal: true
  2. def get_input(prompt = '--> ')
  3. puts ''
  4. puts 'Please enter a number, odd or even.'
  5. print prompt
  6. # if the end-of-input was reached and gets returned a nil
  7. # or the word 'exit' or 'quit' appears (ignoring case) in the input
  8. if (input = gets).nil? || /exit|quit/i =~ input
  9. nil # just return a nil
  10. else
  11. input.to_i # otherwise, return the integer conversion
  12. end
  13. end
  14.  
  15. def test_user_input(number)
  16. "'#{number}' is an #{number.even? ? 'EVEN' : 'odd'} number.\n"
  17. end
  18.  
  19. while (number = get_input) # while the number I capture from get_input isn't nil (or false)
  20. puts test_user_input(number)
  21. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement