Sha256: 9d8ba8c628eb4a11177798bbdd237cbd2b1ca380d8313016e7d5c722f709f4e9

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module Whitespace
  class Console
    attr_reader :stdin, :stdout

    def initialize(stdin: $stdin, stdout: $stdout)
      @stdin = stdin
      @stdout = stdout
    end

    def printc(n)
      unless Util.is_ascii?(n)
        raise ArgumentError, "must be an ASCII character: #{n}"
      end
      stdout.print n.chr
    end

    def printn(n)
      unless Util.is_integer?(n)
        raise ArgumentError, "must be an integer: #{n}"
      end
      stdout.print n
    end

    def getc
      if c = stdin.getc
        unless Util.is_ascii?(c.ord)
          raise ArgumentError, "must be an ASCII character: #{c}"
        end
        c
      else
        raise ArgumentError, "must be an ASCII character: EOF"
      end
    end

    LINE_SEPARATOR = "\n"

    def getn
      input = ""
      loop do
        c = stdin.getc
        break if c.nil?
        input << c
        break if c == LINE_SEPARATOR
      end

      raise ArgumentError, "must be an integer: EOF" if input.empty?

      input = input.chomp(LINE_SEPARATOR)
      begin
        Integer input
      rescue
        raise ArgumentError, "must be an integer: #{input}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
whitespace-ruby-1.0.0 lib/whitespace/data_structures/console.rb