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. The attributes are readers for custom = behavior.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Creates a new pair.

Parameters:

  • left (Object)

    The left piece of data.

  • right (Object)

    The right piece of data.



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

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

Instance Attribute Details

#leftObject

Returns The left data.

Returns:

  • (Object)

    The left data.



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

def left
  @left
end

#rightObject

Returns The right data.

Returns:

  • (Object)

    The right data.



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..



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

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

#clonePair

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

Returns:

  • (Pair)

    The new Pair



127
128
129
# File 'lib/data_types/pair.rb', line 127

def clone
  Pair.new(@left.dup, @right.dup)
end

#empty?Boolean

Gets whether the pair is empty.

Returns:

  • (Boolean)

    True if all values are nil.



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

def empty?
  @left.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 (Pair)

    The other Pair

Returns:

  • (Boolean)

    Whether they are equivalent.



78
79
80
# File 'lib/data_types/pair.rb', line 78

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

#flipPair

Flips the pair.

Returns:

  • (Pair)

    A new pair with the left and right values flipped.



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

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

#flip!Object

Flips the pair in place.



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

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

#freezePair

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

Returns:



92
93
94
95
96
97
98
99
# File 'lib/data_types/pair.rb', line 92

def freeze
  unless frozen?
    @left.freeze
    @right.freeze
    @frozen = true
  end
  self
end

#frozen?Boolean

Returns whether self is frozen.

Returns:

  • (Boolean)

    Whether self is frozen.



86
87
88
# File 'lib/data_types/pair.rb', line 86

def frozen?
  @frozen
end

#include?(data) ⇒ Boolean

Returns true if the given object is present in self.

Parameters:

  • data (Object)

Returns:

  • (Boolean)


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

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

#to_aArray<Object>

Gets the Pair as an array.

Returns:

  • (Array<Object>)

    The array with the pair pieces.



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

def to_a
  [@left, @right]
end

#to_hHash<Symbol, Object>

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

Returns:

  • (Hash<Symbol, Object>)

    The hash with the pair pieces.



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

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

#to_sString

Gets a string representation of the pair.

Returns:

  • (String)

    The stringified version of the pair.



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

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