Sha256: 04772d862105ef514dd6b5767c361f37a835bf7a8974d2ce76ebb2ce1dab4b9b

Contents?: true

Size: 964 Bytes

Versions: 3

Compression:

Stored size: 964 Bytes

Contents

module Bi
  def self.input(question = nil, show_response: false, type: "string")
    unless question.nil?
      puts question
    end

    loop do
      var = gets.chomp

      case type
      when "int"
        begin
          var = Integer(var)
        rescue ArgumentError
          puts "Err: Please, type a (integer) number."
          next
        end
      when "float"
        begin
          var = Float(var)
        rescue ArgumentError
          puts "Err: Please, type a (float) number."
          next
        end
      when "bool"
        if var.downcase == "true"
          var = true
        elsif var.downcase == "false"
          var = false
        else
          puts "Err: Please, type 'true' or 'false'."
          next
        end
      when "string"
        var = String(var)
      else
        puts "Err: Undefined type."
      end

      if show_response
        puts "User Response: #{var}"
      end
      return var
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
better_input-0.1.3 lib/better_input.rb
better_input-0.1.2 lib/better_input.rb
better_input-0.1.1 lib/better_input.rb