Class: Triplet
- Inherits:
-
Object
- Object
- Triplet
- Defined in:
- lib/data_types/triplet.rb
Overview
Triplet class represents a set of 3 pieces of data.
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.
-
#empty? ⇒ Boolean
Gets whether the triplet is empty.
-
#flip ⇒ Triplet
Flips the Triplet into a new Triplet.
-
#flip! ⇒ Triplet
Flips the triplet in place.
-
#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 |
# File 'lib/data_types/triplet.rb', line 17 def initialize(left, middle, right) @left = left @middle = middle @right = right 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.
30 31 32 33 34 |
# File 'lib/data_types/triplet.rb', line 30 def clear remove_instance_variable(:@left) remove_instance_variable(:@middle) remove_instance_variable(:@right) end |
#empty? ⇒ Boolean
Gets whether the triplet is empty.
61 62 63 |
# File 'lib/data_types/triplet.rb', line 61 def empty? @left.nil? && @middle.nil? && @right.nil? end |
#flip ⇒ Triplet
Flips the Triplet into a new Triplet.
67 68 69 |
# File 'lib/data_types/triplet.rb', line 67 def flip Triplet.new(@right, @middle, @left) end |
#flip! ⇒ Triplet
Flips the triplet in place.
73 74 75 76 77 78 79 |
# File 'lib/data_types/triplet.rb', line 73 def flip! left = @left right = @right @right = left @left = right self end |
#include?(data) ⇒ Boolean
Returns true if the given object is present in self.
55 56 57 |
# File 'lib/data_types/triplet.rb', line 55 def include?(data) @left == data || @middle == data || @right == data end |
#to_a ⇒ Array<Object>
Gets the Triplet as an array.
38 39 40 |
# File 'lib/data_types/triplet.rb', line 38 def to_a [@left, @middle, @right] end |
#to_h ⇒ Hash<Symbol, Object>
Gets the Triplet as a hash with left, middle, and right keys.
44 45 46 47 48 49 50 |
# File 'lib/data_types/triplet.rb', line 44 def to_h { left: @left, middle: @middle, right: @right } end |
#to_s ⇒ String
Returns The stringified version of the Triplet.
24 25 26 |
# File 'lib/data_types/triplet.rb', line 24 def to_s "#{@left}, #{@middle}, #{@right}" end |