How to resolve the algorithm Ternary logic step by step in the Ruby programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Ternary logic step by step in the Ruby programming language

Table of Contents

Problem Statement

In logic, a three-valued logic (also trivalent, ternary, or trinary logic, sometimes abbreviated 3VL) is any of several many-valued logic systems in which there are three truth values indicating true, false and some indeterminate third value.
This is contrasted with the more commonly known bivalent logics (such as classical sentential or boolean logic) which provide only for true and false. Conceptual form and basic ideas were initially created by Łukasiewicz, Lewis and Sulski. These were then re-formulated by Grigore Moisil in an axiomatic algebraic form, and also extended to n-valued logics in 1945.

Note:   Setun   (Сетунь) was a   balanced ternary   computer developed in 1958 at   Moscow State University.   The device was built under the lead of   Sergei Sobolev   and   Nikolay Brusentsov.   It was the only modern   ternary computer,   using three-valued ternary logic

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Ternary logic step by step in the Ruby programming language

The code defines a ternary logic system using the singleton class MaybeClass and the trit method on TrueClass, FalseClass, and instances of MaybeClass. Ternary logic has three truth values: true, false, and maybe.

Here's a breakdown of the code:

  1. MaybeClass Definition:

    • The MaybeClass is a singleton class that represents the "maybe" truth value.
    • It includes the Singleton module, which ensures that there is only one instance of MaybeClass.
    • It defines a to_s method that returns the string "maybe".
  2. MAYBE Constant:

    • MAYBE is a constant that stores the instance of MaybeClass.
  3. TritMagic Class on TrueClass:

    • TrueClass includes a nested class called TritMagic.
    • TritMagic defines methods for performing ternary logic operations on true values:
      • index: Returns 0 (the index of true in the ternary logic system).
      • !: Returns false.
      • &: Returns other (the other truth value).
      • |: Returns true.
      • ^: Returns false, MAYBE, or true based on the truth value of other.
      • ==: Returns other.
  4. Trit Method on TrueClass:

    • TrueClass defines a trit method that returns an instance of TritMagic.
    • This allows you to perform ternary logic operations on true values, such as:
      !true.trit        # => false
      true.trit & obj   # => obj
      true.trit | obj   # => true
      true.trit ^ obj   # => false, maybe, or true
      true.trit == obj  # => obj
  5. TritMagic Class on MaybeClass:

    • Similar to TritMagic for TrueClass, MaybeClass has its own TritMagic class for performing ternary logic operations on maybe values.
  6. Trit Method on MaybeClass:

    • MaybeClass defines a trit method that returns an instance of its TritMagic class.
    • This allows you to perform ternary logic operations on maybe values, such as:
      !maybe.trit        # => maybe
      maybe.trit & obj   # => maybe or false
      maybe.trit | obj   # => true or maybe
      maybe.trit ^ obj   # => maybe
      maybe.trit == obj  # => maybe
  7. TritMagic Class on FalseClass:

    • FalseClass also has its own TritMagic class for ternary logic operations on false values.
  8. Trit Method on FalseClass:

    • FalseClass defines a trit method that returns an instance of its TritMagic class.
    • This allows you to perform ternary logic operations on false values, such as:
      !false.trit        # => true
      false.trit & obj   # => false
      false.trit | obj   # => obj
      false.trit ^ obj   # => obj
      false.trit == obj  # => false, maybe, or true

Source code in the ruby programming language

# trit.rb - ternary logic
# http://rosettacode.org/wiki/Ternary_logic

require 'singleton'

# MAYBE, the only instance of MaybeClass, enables a system of ternary
# logic using TrueClass#trit, MaybeClass#trit and FalseClass#trit.
#
#  !a.trit      # ternary not
#  a.trit & b   # ternary and
#  a.trit | b   # ternary or
#  a.trit ^ b   # ternary exclusive or
#  a.trit == b  # ternary equal
#
# Though +true+ and +false+ are internal Ruby values, +MAYBE+ is not.
# Programs may want to assign +maybe = MAYBE+ in scopes that use
# ternary logic. Then programs can use +true+, +maybe+ and +false+.
class MaybeClass
  include Singleton

  #  maybe.to_s  # => "maybe"
  def to_s; "maybe"; end
end

MAYBE = MaybeClass.instance

class TrueClass
  TritMagic = Object.new
  class << TritMagic
    def index; 0; end
    def !; false; end
    def & other; other; end
    def | other; true; end
    def ^ other; [false, MAYBE, true][other.trit.index]; end
    def == other; other; end
  end

  # Performs ternary logic. See MaybeClass.
  #  !true.trit        # => false
  #  true.trit & obj   # => obj
  #  true.trit | obj   # => true
  #  true.trit ^ obj   # => false, maybe or true
  #  true.trit == obj  # => obj
  def trit; TritMagic; end
end

class MaybeClass
  TritMagic = Object.new
  class << TritMagic
    def index; 1; end
    def !; MAYBE; end
    def & other; [MAYBE, MAYBE, false][other.trit.index]; end
    def | other; [true, MAYBE, MAYBE][other.trit.index]; end
    def ^ other; MAYBE; end
    def == other; MAYBE; end
  end

  # Performs ternary logic. See MaybeClass.
  #  !maybe.trit        # => maybe
  #  maybe.trit & obj   # => maybe or false
  #  maybe.trit | obj   # => true or maybe
  #  maybe.trit ^ obj   # => maybe
  #  maybe.trit == obj  # => maybe
  def trit; TritMagic; end
end

class FalseClass
  TritMagic = Object.new
  class << TritMagic
    def index; 2; end
    def !; true; end
    def & other; false; end
    def | other; other; end
    def ^ other; other; end
    def == other; [false, MAYBE, true][other.trit.index]; end
  end

  # Performs ternary logic. See MaybeClass.
  #  !false.trit        # => true
  #  false.trit & obj   # => false
  #  false.trit | obj   # => obj
  #  false.trit ^ obj   # => obj
  #  false.trit == obj  # => false, maybe or true
  def trit; TritMagic; end
end


$ irb
irb(main):001:0> require './trit'
=> true
irb(main):002:0> maybe = MAYBE
=> maybe
irb(main):003:0> !true.trit       
=> false
irb(main):004:0> !maybe.trit
=> maybe
irb(main):005:0> maybe.trit & false
=> false
irb(main):006:0> maybe.trit | true
=> true
irb(main):007:0> false.trit == true       
=> false
irb(main):008:0> false.trit == maybe
=> maybe


require 'trit'
maybe = MAYBE

[true, maybe, false].each do |a|
  [true, maybe, false].each do |b|
    printf "%5s ^ %5s => %5s\n", a, b, a.trit ^ b
  end
end


  

You may also check:How to resolve the algorithm 99 bottles of beer step by step in the Ruby programming language
You may also check:How to resolve the algorithm Greatest element of a list step by step in the Ruby programming language
You may also check:How to resolve the algorithm Tau number step by step in the Ruby programming language
You may also check:How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the Ruby programming language
You may also check:How to resolve the algorithm Vigenère cipher step by step in the Ruby programming language