Class: Symbolic::Variable

Inherits:
Object
  • Object
show all
Includes:
Symbolic
Defined in:
lib/symbolic/variable.rb

Overview

This class is used to create symbolic variables.

Symbolic variables presented by name and value.
Name is neccessary for printing meaningful symbolic expressions.
Value is neccesary for calculation of symbolic expressions.
If value isn't set for variable, but there is an associated proc, then value is taken from evaluating the proc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Symbolic

#*, #**, #+, #+@, #-, #-@, #/, #coerce, #inspect, #operations

Constructor Details

#initialize(options = {}, &proc) ⇒ Variable

Returns a new instance of Variable.



14
15
16
17
18
# File 'lib/symbolic/variable.rb', line 14

def initialize(options={}, &proc)
  @name, @value = options.values_at(:name, :value)
  @name = @name.to_s if @name
  @proc = proc
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/symbolic/variable.rb', line 11

def name
  @name
end

#procObject

Returns the value of attribute proc.



11
12
13
# File 'lib/symbolic/variable.rb', line 11

def proc
  @proc
end

#valueObject



20
21
22
# File 'lib/symbolic/variable.rb', line 20

def value
  @value || @proc && @proc.call.value
end

Instance Method Details

#to_sObject



24
25
26
# File 'lib/symbolic/variable.rb', line 24

def to_s
  @name || 'unnamed_variable'
end

#variablesObject



28
29
30
# File 'lib/symbolic/variable.rb', line 28

def variables
  [self]
end