Module: Nutella
- Defined in:
- lib/nutella/input.rb,
lib/nutella/version.rb
Constant Summary collapse
- VERSION =
The current version of Nutella.
"0.13.1"
Class Method Summary collapse
-
.input(prompt) ⇒ String
Takes input as described in
Nutella#raw_input
, but strips all leading and trailing whitespace. -
.int_input(prompt, err_msg = "Please enter a number.", repeat = true) ⇒ Integer
Takes an integer input from the user.
-
.raw_input(prompt) ⇒ String
Prints the given
prompt
to STDOUT, then reads text from STDIN until the user presses Enter.
Class Method Details
.input(prompt) ⇒ String
Takes input as described in Nutella#raw_input
, but strips all leading and trailing whitespace.
26 27 28 |
# File 'lib/nutella/input.rb', line 26 def input(prompt) raw_input(prompt).strip end |
.int_input(prompt, err_msg = "Please enter a number.", repeat = true) ⇒ Integer
Takes an integer input from the user. If the user does not enter a valid integer value, display the error and ask them again, unless err_msg
is nil
, in which case it will not display any error message before asking again. If repeat
is set to false, it will only ask once, exiting and returning nil if the first attempt fails.
45 46 47 48 49 50 51 52 53 |
# File 'lib/nutella/input.rb', line 45 def int_input(prompt, err_msg = "Please enter a number.", repeat = true) str = "" until str[/^-?\d+$/] str = input prompt puts err_msg unless str[/^-?\d+$/] || err_msg.nil? return unless repeat end str.to_i end |
.raw_input(prompt) ⇒ String
Prints the given prompt
to STDOUT, then reads text from STDIN until the user presses Enter.
16 17 18 19 |
# File 'lib/nutella/input.rb', line 16 def raw_input(prompt) print prompt STDIN.gets.chomp end |