Class: Pair

Inherits:
Object
  • Object
show all
Defined in:
lib/data_types/pair.rb

Overview

The Pair class represents a pair of two pieces of data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left, right) ⇒ Pair<Object, Object>

Creates a new pair.



14
15
16
17
# File 'lib/data_types/pair.rb', line 14

def initialize(left, right)
  @left = left
  @right = right
end

Instance Attribute Details

#leftObject



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

def left
  @left
end

#rightObject



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

def right
  @right
end

Instance Method Details

#clearvoid

This method returns an undefined value.

Removes all of the values in the pair..



27
28
29
30
# File 'lib/data_types/pair.rb', line 27

def clear
  remove_instance_variable(:@left)
  remove_instance_variable(:@right)
end

#empty?Boolean

Gets whether the pair is empty.



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

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

#flipPair

Flips the pair.



62
63
64
# File 'lib/data_types/pair.rb', line 62

def flip
  Pair.new(@right, @left)
end

#flip!Pair

Flips the pair in place.



68
69
70
71
72
73
74
# File 'lib/data_types/pair.rb', line 68

def flip!
  right = @right
  left = @left
  @left = right
  @right = left
  self
end

#include?(data) ⇒ Boolean

Returns true if the given object is present in self.



50
51
52
# File 'lib/data_types/pair.rb', line 50

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

#to_aArray<Object>

Gets the Pair as an array.



34
35
36
# File 'lib/data_types/pair.rb', line 34

def to_a
  [@left, @right]
end

#to_hHash<Symbol, Object>

Gets the Pair as a hash with left and right keys.



40
41
42
43
44
45
# File 'lib/data_types/pair.rb', line 40

def to_h
  {
    left: @left,
    right: @right
  }
end

#to_sString

Gets a string representation of the pair.



21
22
23
# File 'lib/data_types/pair.rb', line 21

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