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.

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
# File 'lib/data_types/triplet.rb', line 17

def initialize(left, middle, right)
  @left = left
  @middle = middle
  @right = right
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.



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.

Returns:

  • (Boolean)

    True if all values are nil.



61
62
63
# File 'lib/data_types/triplet.rb', line 61

def empty?
  @left.nil? && @middle.nil? && @right.nil?
end

#flipTriplet

Flips the Triplet into a new Triplet.

Returns:

  • (Triplet)

    The new triplet with the flipped data.



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.

Returns:

  • (Triplet)

    The Triplet with the left and right values flipped.



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.

Parameters:

  • data (Object)

Returns:

  • (Boolean)


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

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.



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

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.



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_sString

Returns The stringified version of the Triplet.

Returns:

  • (String)

    The stringified version of the Triplet.



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

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