Class: LearnLab::Prompt

Inherits:
Object
  • Object
show all
Defined in:
lib/learn_lab/prompt.rb

Overview

Special prompt that keeps asking for input until it is valid.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = $stdout) ⇒ Prompt

Returns a new instance of Prompt.

[View source]

9
10
11
# File 'lib/learn_lab/prompt.rb', line 9

def initialize(output=$stdout)
  @output = output
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.


7
8
9
# File 'lib/learn_lab/prompt.rb', line 7

def output
  @output
end

Instance Method Details

#ask(question, &block) ⇒ Object

[View source]

13
14
15
16
17
18
19
20
21
22
# File 'lib/learn_lab/prompt.rb', line 13

def ask(question, &block)
  input_is_valid = false
  until input_is_valid
    output.print("\n#{question} > ")
    answer = $stdin.gets.chomp
    input_is_valid = block_given? ? block.call(answer) : true
  end

  answer
end