Class: Electr::ElectrValue

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

Overview

ElectrValue is the container of an evaluation’s result.

The result of the evaluation step can be of three types:

  • number

  • error

  • hidden

The type number is the most common one. It’s, you know, just a number. It’s the result of the evaluation of ‘2k 3`, which is the number 6000. An ElectrValue of type number is meant to be printed on the screen.

If an evaluation raised an error, it produced an ElectrValue of type error. This ElectrValue holds the error message.

The type hidden is just a special case of a number. It is produced as the result of the evaluation of an assignment. That is, ‘R1 = 100` produces an ElectrValue of type hidden. It holds the number 100 but is not likely to be printed on the screen, thus the type hidden.

Creating a new ElectrValue is done using class constructors:

ElectrValue.number(100.0)
ElectrValue.hidden(200.0)
ElectrValue.error("Bad syntax error!")

The class has dedicated method to check the various types:

val = ElectrValue.number(100.0)
val.number? #=> true
val.hidden? #=> false
val.error?  #=> false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(number, type = :number, message = "") ⇒ ElectrValue

Returns a new instance of ElectrValue.



49
50
51
52
53
# File 'lib/electr/repl/electr_value.rb', line 49

def initialize(number, type = :number, message = "")
  @number = number
  @type = type
  @error = message
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



56
57
58
# File 'lib/electr/repl/electr_value.rb', line 56

def error
  @error
end

#numberObject (readonly)

Returns the value of attribute number.



56
57
58
# File 'lib/electr/repl/electr_value.rb', line 56

def number
  @number
end

#typeObject (readonly)

Returns the value of attribute type.



56
57
58
# File 'lib/electr/repl/electr_value.rb', line 56

def type
  @type
end

Class Method Details

.error(message) ⇒ Object



41
42
43
# File 'lib/electr/repl/electr_value.rb', line 41

def self.error(message)
  new(0, :error, message)
end

.hidden(number) ⇒ Object



45
46
47
# File 'lib/electr/repl/electr_value.rb', line 45

def self.hidden(number)
  new(number, :hidden)
end

.number(number) ⇒ Object



37
38
39
# File 'lib/electr/repl/electr_value.rb', line 37

def self.number(number)
  new(number)
end

Instance Method Details

#==(other) ⇒ Object

Say it’s equal only if all three fields are equals. This is enough for now and there is no needs to check equality type by type.



72
73
74
75
# File 'lib/electr/repl/electr_value.rb', line 72

def ==(other)
  return false unless other.is_a?(ElectrValue)
  @number == other.number && @type == other.type && @error = other.error
end

#error?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/electr/repl/electr_value.rb', line 62

def error?
  @type == :error
end

#hidden?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/electr/repl/electr_value.rb', line 66

def hidden?
  @type == :hidden
end

#number?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/electr/repl/electr_value.rb', line 58

def number?
  @type == :number
end