Copyright (c) 2012 Alex Suraci
Ask a question and get an answer.
See Interact#read_line for the other possible values in options.
The prompt, without ": " at the end.
An optional hash containing the following options.
default |
The default value, also used to attempt type conversion of the answer (e.g. numeric/boolean). |
choices |
An array (or Enumerable) of strings to choose from. |
indexed |
Use alternative choice listing, and allow choosing by number. Good for when there are many choices or choices with long names. |
# File lib/interact/interactive.rb, line 172 def ask(question, options = {}) choices = options[:choices] && options[:choices].to_a list_choices(choices, options) if choices while true prompt(question, options) ok, res = answered(read_line(options), options) return res if ok end end
Read a single character.
An optional hash containing the following options.
input |
The input source (defaults to $stdin). |
# File lib/interact/interactive.rb, line 109 def read_char(options = {}) input = options[:input] || $stdin with_char_io(input) do get_character(input) end end
Read a single event.
An optional hash containing the following options.
input |
The input source (defaults to $stdin). |
# File lib/interact/interactive.rb, line 123 def read_event(options = {}) input = options[:input] || $stdin with_char_io(input) do get_event(input) end end
Read a line of input.
An optional hash containing the following options.
input |
The input source (defaults to $stdin). |
echo |
A string to echo when showing the input; used for things like hiding password input. |
# File lib/interact/interactive.rb, line 141 def read_line(options = {}) input = options[:input] || $stdin state = input_state(options) with_char_io(input) do until state.done? handler(get_event(input), state) end end state.answer end
Generated with the Darkfish Rdoc Generator 2.