class Object

Public Instance Methods

ask_valid(title) { |input| ... } click to toggle source
# File lib/wotusay.rb, line 1
def ask_valid(title)
  value = nil
  loop do
    puts "Enter #{ title }:"
    print '> '
    input = gets.strip # Strip whitespace

    begin
      value = yield input 
    rescue => e
      puts "Error: #{e.message}"
      value = nil
    end
    
    break unless value.nil?

    puts "* Invalid #{ title } *"
  end
  
  value
end
ask_valid_string(title) click to toggle source
# File lib/wotusay.rb, line 23
def ask_valid_string(title)
  ask_valid(title) do |input|
    next nil if input.empty?
    input
  end
end