Class: Electr::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/electr/repl/printer.rb

Overview

It knows how to print the result of an evaluation.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ Printer

Returns a new instance of Printer.



11
12
13
# File 'lib/electr/repl/printer.rb', line 11

def initialize(result)
  @result = result
end

Class Method Details

.run(result) ⇒ Object

result - The ElectrValue result of an evaluation.



7
8
9
# File 'lib/electr/repl/printer.rb', line 7

def self.run(result)
  new(result).print
end

Instance Method Details



15
16
17
18
19
20
21
22
# File 'lib/electr/repl/printer.rb', line 15

def print
  if @result.number?
    print_number
  elsif @result.error?
    print_error
  end
  # If hidden, print nothing.
end


30
31
32
# File 'lib/electr/repl/printer.rb', line 30

def print_error
  puts @result.error
end


24
25
26
27
28
# File 'lib/electr/repl/printer.rb', line 24

def print_number
  number = @result.number
  truncated = number.truncate
  puts number == truncated ? truncated : number.round(10)
end