Class: Electr::Printer
- Inherits:
-
Object
- Object
- Electr::Printer
- Defined in:
- lib/electr/repl/printer.rb
Overview
It knows how to print the result of an evaluation.
Class Method Summary collapse
-
.run(result) ⇒ Object
result - The ElectrValue result of an evaluation.
Instance Method Summary collapse
-
#initialize(result) ⇒ Printer
constructor
A new instance of Printer.
- #print ⇒ Object
- #print_error ⇒ Object
- #print_number ⇒ Object
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
#print ⇒ Object
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 |
#print_error ⇒ Object
30 31 32 |
# File 'lib/electr/repl/printer.rb', line 30 def print_error puts @result.error end |
#print_number ⇒ Object
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 |