data_types 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/data_types.rb +2 -0
- data/lib/data_types/pair.rb +75 -0
- data/lib/data_types/triplet.rb +80 -0
- data/spec/pair_test_cases.rb +53 -0
- data/spec/triplet_test_cases.rb +57 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d10672d37ed9a9d9078f149f3b5461b0832d87af
|
4
|
+
data.tar.gz: 35f5997085de9732c2de7b2a65c976a9a28cc7f1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 895dde9c53db720c9daf23bf8dfd2610d9ffd276f1cb505154b7bf3901d689ba7f6bc288633cf6512f450494e56845018269e43b38aa15382edbb190eee33d29
|
7
|
+
data.tar.gz: 9948ee9c1429accbdcf45d30a2df553d68da1ad7651468b6250ff58e7b59adab62f4af44c543c3ff5cfbefde087763ebfaec0d893634bea43e311e9a8934c5de
|
data/lib/data_types.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# The Pair class represents a pair of two pieces of data.
|
2
|
+
class Pair
|
3
|
+
# @return [Object] The left data.
|
4
|
+
attr_accessor :left
|
5
|
+
|
6
|
+
# @return [Object] The right data.
|
7
|
+
attr_accessor :right
|
8
|
+
|
9
|
+
# Creates a new pair.
|
10
|
+
# @param left [Object] The left piece of data.
|
11
|
+
# @param right [Object] The right piece of data.
|
12
|
+
# @return [Pair<Object, Object>] The new pair created with these pieces of
|
13
|
+
# data.
|
14
|
+
def initialize(left, right)
|
15
|
+
@left = left
|
16
|
+
@right = right
|
17
|
+
end
|
18
|
+
|
19
|
+
# Gets a string representation of the pair.
|
20
|
+
# @return [String] The stringified version of the pair.
|
21
|
+
def to_s
|
22
|
+
"#{@left}, #{@right}"
|
23
|
+
end
|
24
|
+
|
25
|
+
# Removes all of the values in the pair..
|
26
|
+
# @return [void]
|
27
|
+
def clear
|
28
|
+
remove_instance_variable(:@left)
|
29
|
+
remove_instance_variable(:@right)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Gets the Pair as an array.
|
33
|
+
# @return [Array<Object>] The array with the pair pieces.
|
34
|
+
def to_a
|
35
|
+
[@left, @right]
|
36
|
+
end
|
37
|
+
|
38
|
+
# Gets the Pair as a hash with left and right keys.
|
39
|
+
# @return [Hash<Symbol, Object>] The hash with the pair pieces.
|
40
|
+
def to_h
|
41
|
+
{
|
42
|
+
left: @left,
|
43
|
+
right: @right
|
44
|
+
}
|
45
|
+
end
|
46
|
+
|
47
|
+
# Returns true if the given object is present in self.
|
48
|
+
# @param data [Object]
|
49
|
+
# @return [Boolean]
|
50
|
+
def include?(data)
|
51
|
+
@left == data || @right == data
|
52
|
+
end
|
53
|
+
|
54
|
+
# Gets whether the pair is empty.
|
55
|
+
# @return [Boolean] True if all values are nil.
|
56
|
+
def empty?
|
57
|
+
@left.nil? && @right.nil?
|
58
|
+
end
|
59
|
+
|
60
|
+
# Flips the pair.
|
61
|
+
# @return [Pair] A new pair with the left and right values flipped.
|
62
|
+
def flip
|
63
|
+
Pair.new(@right, @left)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Flips the pair in place.
|
67
|
+
# @return [Pair] Returns self.
|
68
|
+
def flip!
|
69
|
+
right = @right
|
70
|
+
left = @left
|
71
|
+
@left = right
|
72
|
+
@right = left
|
73
|
+
self
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# Triplet class represents a set of 3 pieces of data.
|
2
|
+
class Triplet
|
3
|
+
# @return [Object] The first data piece.
|
4
|
+
attr_accessor :left
|
5
|
+
|
6
|
+
# @return [Object] The second data piece.
|
7
|
+
attr_accessor :middle
|
8
|
+
|
9
|
+
# @return [Object] The third data piece.
|
10
|
+
attr_accessor :right
|
11
|
+
|
12
|
+
# Creates a new Triplet.
|
13
|
+
# @param left [Object] The first data.
|
14
|
+
# @param middle [Object] The middle data.
|
15
|
+
# @param right [Object] The last data.
|
16
|
+
# @return [Triplet<Object, Object, Object>] The new Triplet.
|
17
|
+
def initialize(left, middle, right)
|
18
|
+
@left = left
|
19
|
+
@middle = middle
|
20
|
+
@right = right
|
21
|
+
end
|
22
|
+
|
23
|
+
# @return [String] The stringified version of the Triplet.
|
24
|
+
def to_s
|
25
|
+
"#{@left}, #{@middle}, #{@right}"
|
26
|
+
end
|
27
|
+
|
28
|
+
# Removes all of the values in the triplet.
|
29
|
+
# @return [void]
|
30
|
+
def clear
|
31
|
+
remove_instance_variable(:@left)
|
32
|
+
remove_instance_variable(:@middle)
|
33
|
+
remove_instance_variable(:@right)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Gets the Triplet as an array.
|
37
|
+
# @return [Array<Object>] The array with the triplet pieces.
|
38
|
+
def to_a
|
39
|
+
[@left, @middle, @right]
|
40
|
+
end
|
41
|
+
|
42
|
+
# Gets the Triplet as a hash with left, middle, and right keys.
|
43
|
+
# @return [Hash<Symbol, Object>] The hash with the triplet pieces.
|
44
|
+
def to_h
|
45
|
+
{
|
46
|
+
left: @left,
|
47
|
+
middle: @middle,
|
48
|
+
right: @right
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
# Returns true if the given object is present in self.
|
53
|
+
# @param data [Object]
|
54
|
+
# @return [Boolean]
|
55
|
+
def include?(data)
|
56
|
+
@left == data || @middle == data || @right == data
|
57
|
+
end
|
58
|
+
|
59
|
+
# Gets whether the triplet is empty.
|
60
|
+
# @return [Boolean] True if all values are nil.
|
61
|
+
def empty?
|
62
|
+
@left.nil? && @middle.nil? && @right.nil?
|
63
|
+
end
|
64
|
+
|
65
|
+
# Flips the Triplet into a new Triplet.
|
66
|
+
# @return [Triplet] The new triplet with the flipped data.
|
67
|
+
def flip
|
68
|
+
Triplet.new(@right, @middle, @left)
|
69
|
+
end
|
70
|
+
|
71
|
+
# Flips the triplet in place.
|
72
|
+
# @return [Triplet] The Triplet with the left and right values flipped.
|
73
|
+
def flip!
|
74
|
+
left = @left
|
75
|
+
right = @right
|
76
|
+
@right = left
|
77
|
+
@left = right
|
78
|
+
self
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require_relative '../lib/data_types/pair'
|
3
|
+
|
4
|
+
class PairTestCases < Test::Unit::TestCase
|
5
|
+
def test_string
|
6
|
+
pair = Pair.new(1, 2)
|
7
|
+
assert_equal('1, 2', pair.to_s)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_array
|
11
|
+
pair = Pair.new(1, 2)
|
12
|
+
assert_equal([1, 2], pair.to_a)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_hash
|
16
|
+
pair = Pair.new(1, 2)
|
17
|
+
assert_equal({ left: 1, right: 2 }, pair.to_h)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_include?
|
21
|
+
pair = Pair.new(1, 2)
|
22
|
+
assert_equal(true, pair.include?(1))
|
23
|
+
assert_equal(true, pair.include?(2))
|
24
|
+
assert_equal(false, pair.include?(3))
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_flip
|
28
|
+
pair = Pair.new(1, 2)
|
29
|
+
assert_equal(1, pair.flip.right)
|
30
|
+
assert_equal(2, pair.flip.left)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_flip!
|
34
|
+
pair = Pair.new(1, 2)
|
35
|
+
pair.flip!
|
36
|
+
assert_equal(1, pair.right)
|
37
|
+
assert_equal(2, pair.left)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_empty?
|
41
|
+
pair = Pair.new(1, 2)
|
42
|
+
assert_equal(false, pair.empty?)
|
43
|
+
pair.clear
|
44
|
+
assert_equal(true, pair.empty?)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_clear
|
48
|
+
pair = Pair.new(1, 2)
|
49
|
+
pair.clear
|
50
|
+
assert_nil(pair.left)
|
51
|
+
assert_nil(pair.right)
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require_relative '../lib/data_types/triplet'
|
3
|
+
|
4
|
+
class TripletTestCases < Test::Unit::TestCase
|
5
|
+
def test_string
|
6
|
+
triplet = Triplet.new(1, 2, 3)
|
7
|
+
assert_equal('1, 2, 3', triplet.to_s)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_array
|
11
|
+
triplet = Triplet.new(1, 2, 3)
|
12
|
+
assert_equal([1, 2, 3], triplet.to_a)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_hash
|
16
|
+
triplet = Triplet.new(1, 2, 3)
|
17
|
+
assert_equal({ left: 1, middle: 2, right: 3 }, triplet.to_h)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_include?
|
21
|
+
triplet = Triplet.new(1, 2, 3)
|
22
|
+
assert_equal(true, triplet.include?(1))
|
23
|
+
assert_equal(true, triplet.include?(2))
|
24
|
+
assert_equal(true, triplet.include?(3))
|
25
|
+
assert_equal(false, triplet.include?(4))
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_flip
|
29
|
+
triplet = Triplet.new(1, 2, 3)
|
30
|
+
assert_equal(1, triplet.flip.right)
|
31
|
+
assert_equal(2, triplet.flip.middle)
|
32
|
+
assert_equal(3, triplet.flip.left)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_flip!
|
36
|
+
triplet = Triplet.new(1, 2, 3)
|
37
|
+
triplet.flip!
|
38
|
+
assert_equal(1, triplet.right)
|
39
|
+
assert_equal(2, triplet.middle)
|
40
|
+
assert_equal(3, triplet.left)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_empty?
|
44
|
+
triplet = Triplet.new(1, 2, 3)
|
45
|
+
assert_equal(false, triplet.empty?)
|
46
|
+
triplet.clear
|
47
|
+
assert_equal(true, triplet.empty?)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_clear
|
51
|
+
triplet = Triplet.new(1, 2, 3)
|
52
|
+
triplet.clear
|
53
|
+
assert_nil(triplet.left)
|
54
|
+
assert_nil(triplet.middle)
|
55
|
+
assert_nil(triplet.right)
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: data_types
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eli Foster
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-26 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email: elifosterwy@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/data_types.rb
|
20
|
+
- lib/data_types/pair.rb
|
21
|
+
- lib/data_types/triplet.rb
|
22
|
+
- spec/pair_test_cases.rb
|
23
|
+
- spec/triplet_test_cases.rb
|
24
|
+
homepage: https://github.com/elifoster/data_types
|
25
|
+
licenses:
|
26
|
+
- CC-BY-NC-ND-4.0
|
27
|
+
metadata:
|
28
|
+
issue_tracker: https://github.com/elifoster/data_types/issues
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubyforge_project:
|
45
|
+
rubygems_version: 2.4.5.1
|
46
|
+
signing_key:
|
47
|
+
specification_version: 4
|
48
|
+
summary: A Ruby gem that adds new object types like pairs and triplets.
|
49
|
+
test_files: []
|
50
|
+
has_rdoc:
|