Class: Whitespace::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/whitespace/data_structures/console.rb

Constant Summary collapse

LINE_SEPARATOR =
"\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stdin: $stdin, stdout: $stdout) ⇒ Console

Returns a new instance of Console.



5
6
7
8
# File 'lib/whitespace/data_structures/console.rb', line 5

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

Instance Attribute Details

#stdinObject (readonly)

Returns the value of attribute stdin.



3
4
5
# File 'lib/whitespace/data_structures/console.rb', line 3

def stdin
  @stdin
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



3
4
5
# File 'lib/whitespace/data_structures/console.rb', line 3

def stdout
  @stdout
end

Instance Method Details

#getcObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/whitespace/data_structures/console.rb', line 24

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

#getnObject

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/whitespace/data_structures/console.rb', line 37

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

#printc(n) ⇒ Object



10
11
12
13
14
15
# File 'lib/whitespace/data_structures/console.rb', line 10

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

#printn(n) ⇒ Object



17
18
19
20
21
22
# File 'lib/whitespace/data_structures/console.rb', line 17

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