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.
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..
-
#empty? ⇒ Boolean
Gets whether the pair is empty.
-
#flip ⇒ Pair
Flips the pair.
-
#flip! ⇒ Pair
Flips the pair in place.
-
#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 |
# File 'lib/data_types/pair.rb', line 14 def initialize(left, right) @left = left @right = right end |
Instance Attribute Details
#left ⇒ Object
4 5 6 |
# File 'lib/data_types/pair.rb', line 4 def left @left end |
#right ⇒ Object
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..
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 |
#flip ⇒ Pair
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_a ⇒ Array<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_h ⇒ Hash<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_s ⇒ String
Gets a string representation of the pair.
21 22 23 |
# File 'lib/data_types/pair.rb', line 21 def to_s "#{@left}, #{@right}" end |