class TableOfTruth::Table

Attributes

expression_names[R]

@return [Array<String or Symbol>]

expressions[R]

@return [Hash<(String or Symbol)->Proc>]

input_names[R]

@return [Array<String or Symbol>]

inputs[R]

@return [Hash<(String or Symbol)->Array>]

Public Class Methods

new(&block) click to toggle source
# File lib/table_of_truth/table.rb, line 17
def initialize(&block)
  dsl = DSL.new
  dsl.instance_eval(&block)

  @inputs = dsl.inputs
  @input_names = dsl.input_names
  @expressions = dsl.expressions
  @expression_names = dsl.expression_names
end

Public Instance Methods

equivalent?() click to toggle source

@return [Boolean]

# File lib/table_of_truth/table.rb, line 63
def equivalent?
  results.all? { |result| result.values_at(*expression_names).uniq.length == 1 }
end
print!(colorize: true) click to toggle source

@return [void]

results() click to toggle source

@return [Array<Hash{String->Boolean}>]

# File lib/table_of_truth/table.rb, line 28
def results
  @results ||= input_table.map do |inputs|
    result = inputs.dup
    expressions.each { |name, expression| result[name] = Inputs.new(inputs).instance_eval(&expression) }

    result
  end
end

Private Instance Methods

input_table(names = input_names) click to toggle source

@param names [Array<>] @return [Array<Hash{String->Boolean}>]

# File lib/table_of_truth/table.rb, line 82
def input_table(names = input_names)
  return [{}] if names.empty?

  names = names.dup
  name = names.shift
  table = input_table(names)
  inputs[name].each_with_object([]) do |value, lines|
    lines.concat(table.map { |line| line.merge(name => value) })
  end
end
output_string(value) click to toggle source

@param value [Object] @return [String]

# File lib/table_of_truth/table.rb, line 71
def output_string(value)
  case value
  when true then '✔'
  when false then '✗'
  when nil then '-'
  else value.to_s
  end
end