Files

Class/Module Index [+]

Quicksearch

Interactive

Copyright (c) 2012 Alex Suraci

Constants

ESCAPES
EVENTS

Public Instance Methods

ask(question, options = {}) click to toggle source

Ask a question and get an answer.

See Interact#read_line for the other possible values in options.

question

The prompt, without ": " at the end.

options

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 171
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_char(options = {}) click to toggle source

Read a single character.

options

An optional hash containing the following options.

input

The input source (defaults to $stdin).

# File lib/interact/interactive.rb, line 108
def read_char(options = {})
  input = options[:input] || $stdin

  with_char_io(input) do
    get_character(input)
  end
end
read_event(options = {}) click to toggle source

Read a single event.

options

An optional hash containing the following options.

input

The input source (defaults to $stdin).

# File lib/interact/interactive.rb, line 122
def read_event(options = {})
  input = options[:input] || $stdin

  with_char_io(input) do
    get_event(input)
  end
end
read_line(options = {}) click to toggle source

Read a line of input.

options

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 140
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

[Validate]

Generated with the Darkfish Rdoc Generator 2.