Class: Pair
- Inherits:
-
Object
- Object
- Pair
- 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
-
#left ⇒ Object
The left data.
-
#right ⇒ Object
The right data.
Instance Method Summary collapse
-
#clear ⇒ void
Removes all of the values in the pair..
-
#clone ⇒ Pair
Returns a new copied version of the Pair, with copied values.
-
#empty? ⇒ Boolean
Gets whether the pair is empty.
-
#eql?(other) ⇒ Boolean
(also: #==)
Returns true if self and other have the same left and right values.
-
#flip ⇒ Pair
Flips the pair.
-
#flip! ⇒ Object
Flips the pair in place.
-
#freeze ⇒ Pair
Deep-freezes the Pair, freezing left and right, as well as the object itself.
-
#frozen? ⇒ Boolean
Returns whether self is frozen.
-
#include?(data) ⇒ Boolean
Returns true if the given object is present in self.
-
#initialize(left, right) ⇒ Pair<Object, Object>
constructor
Creates a new pair.
-
#to_a ⇒ Array<Object>
Gets the Pair as an array.
-
#to_h ⇒ Hash<Symbol, Object>
Gets the Pair as a hash with left and right keys.
-
#to_s ⇒ String
Gets a string representation of the pair.
Constructor Details
#initialize(left, right) ⇒ Pair<Object, Object>
Creates a new pair.
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
#left ⇒ Object
Returns The left data.
4 5 6 |
# File 'lib/data_types/pair.rb', line 4 def left @left end |
#right ⇒ Object
Returns The right data.
7 8 9 |
# File 'lib/data_types/pair.rb', line 7 def right @right end |
Instance Method Details
#clear ⇒ void
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 |
#clone ⇒ Pair
Returns a new copied version of the Pair, with copied values.
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.
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.
78 79 80 |
# File 'lib/data_types/pair.rb', line 78 def eql?(other) @left == other.left && @right == other.right end |
#flip ⇒ Pair
Flips the pair.
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 |
#freeze ⇒ Pair
Deep-freezes the Pair, freezing left and right, as well as the object itself.
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.
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.
51 52 53 |
# File 'lib/data_types/pair.rb', line 51 def include?(data) @left == data || @right == data end |
#to_a ⇒ Array<Object>
Gets the Pair as an array.
35 36 37 |
# File 'lib/data_types/pair.rb', line 35 def to_a [@left, @right] end |
#to_h ⇒ Hash<Symbol, Object>
Gets the Pair as a hash with left and right keys.
41 42 43 44 45 46 |
# File 'lib/data_types/pair.rb', line 41 def to_h { left: @left, right: @right } end |
#to_s ⇒ String
Gets a string representation of the pair.
22 23 24 |
# File 'lib/data_types/pair.rb', line 22 def to_s "#{@left}, #{@right}" end |