Class: Triplet

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(left, middle, right) ⇒ Triplet<Object, Object, Object>

Creates a new Triplet.

Parameters:

  • left (Object)

    The first data.

  • middle (Object)

    The middle data.

  • right (Object)

    The last data.



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

#leftObject

Returns The first data piece.

Returns:

  • (Object)

    The first data piece.



4
5
6
# File 'lib/data_types/triplet.rb', line 4

def left
  @left
end

#middleObject

Returns The second data piece.

Returns:

  • (Object)

    The second data piece.



7
8
9
# File 'lib/data_types/triplet.rb', line 7

def middle
  @middle
end

#rightObject

Returns The third data piece.

Returns:

  • (Object)

    The third data piece.



10
11
12
# File 'lib/data_types/triplet.rb', line 10

def right
  @right
end

Instance Method Details

#clearvoid

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

#cloneTriplet

Returns a new copied version of the Triplet, with copied values.

Returns:



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.

Returns:

  • (Boolean)

    True if all values are nil.



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.

Parameters:

  • other (Triplet)

    The other Triplet

Returns:

  • (Boolean)

    Whether they are equivalent.



83
84
85
# File 'lib/data_types/triplet.rb', line 83

def eql?(other)
  @left == other.left &&@middle == other.middle && @right == other.right
end

#flipTriplet

Flips the Triplet into a new Triplet.

Returns:

  • (Triplet)

    The new triplet with the flipped data.



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

#freezeTriplet

Deep-freezes the Triplet, freezing left, middle, and right, as well as the object itself.

Returns:



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.

Returns:

  • (Boolean)

    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.

Parameters:

  • data (Object)

Returns:

  • (Boolean)


56
57
58
# File 'lib/data_types/triplet.rb', line 56

def include?(data)
  @left == data || @middle == data || @right == data
end

#to_aArray<Object>

Gets the Triplet as an array.

Returns:

  • (Array<Object>)

    The array with the triplet pieces.



39
40
41
# File 'lib/data_types/triplet.rb', line 39

def to_a
  [@left, @middle, @right]
end

#to_hHash<Symbol, Object>

Gets the Triplet as a hash with left, middle, and right keys.

Returns:

  • (Hash<Symbol, Object>)

    The hash with the triplet pieces.



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_sString

Returns The stringified version of the Triplet.

Returns:

  • (String)

    The stringified version of the Triplet.



25
26
27
# File 'lib/data_types/triplet.rb', line 25

def to_s
  "#{@left}, #{@middle}, #{@right}"
end