How to resolve the algorithm Ternary logic step by step in the Ruby programming language
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:
-
MaybeClass Definition:
- The
MaybeClassis a singleton class that represents the "maybe" truth value. - It includes the
Singletonmodule, which ensures that there is only one instance ofMaybeClass. - It defines a
to_smethod that returns the string"maybe".
- The
-
MAYBE Constant:
MAYBEis a constant that stores the instance ofMaybeClass.
-
TritMagic Class on TrueClass:
TrueClassincludes a nested class calledTritMagic.TritMagicdefines methods for performing ternary logic operations on true values:index: Returns 0 (the index of true in the ternary logic system).!: Returnsfalse.&: Returnsother(the other truth value).|: Returnstrue.^: Returnsfalse,MAYBE, ortruebased on the truth value ofother.==: Returnsother.
-
Trit Method on TrueClass:
TrueClassdefines atritmethod that returns an instance ofTritMagic.- 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
-
TritMagic Class on MaybeClass:
- Similar to
TritMagicforTrueClass,MaybeClasshas its ownTritMagicclass for performing ternary logic operations on maybe values.
- Similar to
-
Trit Method on MaybeClass:
MaybeClassdefines atritmethod that returns an instance of itsTritMagicclass.- 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
-
TritMagic Class on FalseClass:
FalseClassalso has its ownTritMagicclass for ternary logic operations on false values.
-
Trit Method on FalseClass:
FalseClassdefines atritmethod that returns an instance of itsTritMagicclass.- 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