Class: Triplet
- Inherits:
-
Object
- Object
- Triplet
- Defined in:
- lib/data_types/triplet.rb
Overview
Triplet class represents a set of 3 pieces of data. The attributes are readers for custom = behavior.
Instance Attribute Summary collapse
-
#left ⇒ Object
The first data piece.
-
#middle ⇒ Object
The second data piece.
-
#right ⇒ Object
The third data piece.
Instance Method Summary collapse
-
#clear ⇒ void
Removes all of the values in the triplet.
-
#clone ⇒ Triplet
Returns a new copied version of the Triplet, with copied values.
-
#empty? ⇒ Boolean
Gets whether the triplet is empty.
-
#eql?(other) ⇒ Boolean
(also: #==)
Returns true if self and other have the same left and right values.
-
#flip ⇒ Triplet
Flips the Triplet into a new Triplet.
-
#flip! ⇒ Object
Flips the triplet in place.
-
#freeze ⇒ Triplet
Deep-freezes the Triplet, freezing left, middle, and right, as well as the object itself.
-
#frozen? ⇒ Boolean
Returns whether self is frozen.
-
#include?(data) ⇒ Boolean
Returns true if the given object is present in self.
-
#initialize(left, middle, right) ⇒ Triplet<Object, Object, Object>
constructor
Creates a new Triplet.
-
#to_a ⇒ Array<Object>
Gets the Triplet as an array.
-
#to_h ⇒ Hash<Symbol, Object>
Gets the Triplet as a hash with left, middle, and right keys.
-
#to_s ⇒ String
The stringified version of the Triplet.
Constructor Details
#initialize(left, middle, right) ⇒ Triplet<Object, Object, Object>
Creates a new Triplet.
17 18 19 20 21 22 |
# File 'lib/data_types/triplet.rb', line 17 def initialize(left, middle, right) @left = left @middle = middle @right = right @frozen = false end |
Instance Attribute Details
#left ⇒ Object
Returns The first data piece.
4 5 6 |
# File 'lib/data_types/triplet.rb', line 4 def left @left end |
#middle ⇒ Object
Returns The second data piece.
7 8 9 |
# File 'lib/data_types/triplet.rb', line 7 def middle @middle end |
#right ⇒ Object
Returns The third data piece.
10 11 12 |
# File 'lib/data_types/triplet.rb', line 10 def right @right end |
Instance Method Details
#clear ⇒ void
This method returns an undefined value.
Removes all of the values in the triplet.
31 32 33 34 35 |
# File 'lib/data_types/triplet.rb', line 31 def clear remove_instance_variable(:@left) remove_instance_variable(:@middle) remove_instance_variable(:@right) end |
#clone ⇒ Triplet
Returns a new copied version of the Triplet, with copied values.
145 146 147 |
# File 'lib/data_types/triplet.rb', line 145 def clone Triplet.new(@left.dup, @middle.dup, @right.dup) end |
#empty? ⇒ Boolean
Gets whether the triplet is empty.
62 63 64 |
# File 'lib/data_types/triplet.rb', line 62 def empty? @left.nil? && @middle.nil? && @right.nil? end |
#eql?(other) ⇒ Boolean Also known as: ==
Returns true if self and other have the same left and right values.
83 84 85 |
# File 'lib/data_types/triplet.rb', line 83 def eql?(other) @left == other.left &&@middle == other.middle && @right == other.right end |
#flip ⇒ Triplet
Flips the Triplet into a new Triplet.
68 69 70 |
# File 'lib/data_types/triplet.rb', line 68 def flip Triplet.new(@right, @middle, @left) end |
#flip! ⇒ Object
Flips the triplet in place.
73 74 75 76 77 78 |
# File 'lib/data_types/triplet.rb', line 73 def flip! left = @left right = @right @right = left @left = right end |
#freeze ⇒ Triplet
Deep-freezes the Triplet, freezing left, middle, and right, as well as the object itself.
97 98 99 100 101 102 103 104 105 |
# File 'lib/data_types/triplet.rb', line 97 def freeze unless frozen? @left.freeze @middle.freeze @right.freeze @frozen = true end self end |
#frozen? ⇒ Boolean
Returns whether self is frozen.
91 92 93 |
# File 'lib/data_types/triplet.rb', line 91 def frozen? @frozen end |
#include?(data) ⇒ Boolean
Returns true if the given object is present in self.
56 57 58 |
# File 'lib/data_types/triplet.rb', line 56 def include?(data) @left == data || @middle == data || @right == data end |
#to_a ⇒ Array<Object>
Gets the Triplet as an array.
39 40 41 |
# File 'lib/data_types/triplet.rb', line 39 def to_a [@left, @middle, @right] end |
#to_h ⇒ Hash<Symbol, Object>
Gets the Triplet as a hash with left, middle, and right keys.
45 46 47 48 49 50 51 |
# File 'lib/data_types/triplet.rb', line 45 def to_h { left: @left, middle: @middle, right: @right } end |
#to_s ⇒ String
Returns The stringified version of the Triplet.
25 26 27 |
# File 'lib/data_types/triplet.rb', line 25 def to_s "#{@left}, #{@middle}, #{@right}" end |