Class: LearnLab::Prompt
- Inherits:
-
Object
- Object
- LearnLab::Prompt
- Defined in:
- lib/learn_lab/prompt.rb
Overview
Special prompt that keeps asking for input until it is valid.
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
Returns the value of attribute output.
Instance Method Summary collapse
- #ask(question, &block) ⇒ Object
-
#initialize(output = $stdout) ⇒ Prompt
constructor
A new instance of Prompt.
Constructor Details
permalink #initialize(output = $stdout) ⇒ Prompt
Returns a new instance of Prompt.
9 10 11 |
# File 'lib/learn_lab/prompt.rb', line 9 def initialize(output=$stdout) @output = output end |
Instance Attribute Details
permalink #output ⇒ Object (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
permalink #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 |