class LogicTools::Variable

Represents a logical variable.

Attributes

value[R]

The current value of the variable (boolean).

Public Class Methods

exists?(name) click to toggle source

Checks if variable name exists.

# File lib/logic_tools/logictree.rb, line 44
def Variable.exists?(name)
    return @@variables.has_key?(name.to_s)
end
get(name) click to toggle source

Gets a variable by name. If there is no such variable yet, creates it.

# 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
new(name) click to toggle source

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

# 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

Public Instance Methods

<=>(b) click to toggle source

Compares with another object using the name of the variable.

# File lib/logic_tools/logictree.rb, line 68
def <=>(b)
    self.to_s <=> b.to_s
end
to_s() click to toggle source

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

# File lib/logic_tools/logictree.rb, line 59
def to_s
    @name.dup
end
value=(value) click to toggle source

Sets the variable to value (boolean).

# File lib/logic_tools/logictree.rb, line 35
def value=(value)
    if (value.respond_to?(:to_i))
        @value = value.to_i == 0 ? false : true
    else
        @value = value ? true : false
    end
end