Sha256: 41d622a473cd512c214042d0f3c755e0ae5f36ec5a54606ffd7fc71a0dd1c108

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

class LogicalGate
  def self.not(a)
    return inot(a) if a.is_a? Integer
    return !a
  end

  def self.and(a, b, *others)
    return self.iand(a, b, others) if a.is_a? Integer
    return false if (others.include?(false) || !a || !b)
    return true
  end

  def self.nand(a, b, *others)
    self.not self.and(a, b, others)
  end

  def self.or(a, b, *others)
    return self.ior(a, b, others) if a.is_a? Integer
    return true if others.include?(true) || a || b
    return false
  end

  def self.nor(a, b, *others)
    self.not self.or(a, b, others)
  end

  def self.xor(a, b, *others)
    result = self.base_xor a, b

    others.each do |term|
      result = base_xor(result, term)
    end

    return result
  end

  def self.xnor(a, b, *others)
    return !self.xor(a, b, others)
  end

  private
  def self.base_xor(a, b)
    a != b
  end

  def self.inot(a)
    a == 1? 0 : 1
  end

  def self.iand(a, b, *others)
    return 0 if others.include?(0) || a == 0 || b == 0
    return 1
  end

  def self.ior(a, b, *others)
    return 1 if others.include?(1) || a == 1 || b == 1
    return 0
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ada_truthy-1.0.0 lib/logical_gates/logical_gate.rb