Class: TruthTable

Inherits:
Object
  • Object
show all
Defined in:
lib/logic_analyzer/truth_table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variables) ⇒ TruthTable

Returns a new instance of TruthTable.



4
5
6
7
8
# File 'lib/logic_analyzer/truth_table.rb', line 4

def initialize(variables)
  @variables = variables
  @iterations = 2 ** variables.size
  @table = []
end

Instance Attribute Details

#iterationsObject

Returns the value of attribute iterations.



2
3
4
# File 'lib/logic_analyzer/truth_table.rb', line 2

def iterations
  @iterations
end

#tableObject

Returns the value of attribute table.



2
3
4
# File 'lib/logic_analyzer/truth_table.rb', line 2

def table
  @table
end

#variablesObject

Returns the value of attribute variables.



2
3
4
# File 'lib/logic_analyzer/truth_table.rb', line 2

def variables
  @variables
end

Instance Method Details

#generate_tableObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/logic_analyzer/truth_table.rb', line 10

def generate_table
  for iteration in 0...iterations
    row = []

    for column in (variables.size - 1).downto 0
      value = (iteration / 2 ** column).odd?
      row.append(value)
    end

    table.append(row)
  end

  table
end

#truth_value_of(variable, row) ⇒ Object



25
26
27
28
# File 'lib/logic_analyzer/truth_table.rb', line 25

def truth_value_of(variable, row)
  column = variables.find_index(variable)
  table.at(row).at(column)
end