Class: LogicTools::Variable

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/logic_tools/logictree.rb

Overview

Represents a logical variable.

Constant Summary collapse

@@variables =

The pool of variables

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Variable

Creates a new variable with name (the value is set to false).



23
24
25
26
27
28
29
30
31
32
# File 'lib/logic_tools/logictree.rb', line 23

def initialize(name)
    if @@variables.key?(name.to_s)
        raise "Variable already present."
    end
    # print "New variable with name=#{name}\n"
    @name = name.to_s
    @value = false
    # Add the variable to the pool
    @@variables[name.to_s] = self
end

Instance Attribute Details

#valueObject

The current value of the variable (boolean).



20
21
22
# File 'lib/logic_tools/logictree.rb', line 20

def value
  @value
end

Class Method Details

.exists?(name) ⇒ Boolean

Checks if variable name exists.

Returns:

  • (Boolean)


44
45
46
# File 'lib/logic_tools/logictree.rb', line 44

def Variable.exists?(name)
    return @@variables.has_key?(name.to_s)
end

.get(name) ⇒ Object

Gets a variable by name.

If there is no such variable yet, creates it.


50
51
52
53
54
55
# File 'lib/logic_tools/logictree.rb', line 50

def Variable.get(name)
    var = @@variables[name.to_s]
    # print "Got var=#{var.to_s} with value=#{var.value}\n" if var
    var = Variable.new(name) unless var
    return var
end

Instance Method Details

#<=>(b) ⇒ Object

Compares with another object using the name of the variable.



68
69
70
# File 'lib/logic_tools/logictree.rb', line 68

def <=>(b)
    self.to_s <=> b.to_s
end

#inspectObject

:nodoc:



63
64
65
# File 'lib/logic_tools/logictree.rb', line 63

def inspect # :nodoc:
    to_s
end

#to_sObject

Converts to a string: actually returns a duplicate of the name

of the variable.


59
60
61
# File 'lib/logic_tools/logictree.rb', line 59

def to_s
    @name.dup
end