Eman_Fraction 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/eman_fraction.rb +29 -7
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 965800579a72dbb17f7b371c12b1b8e1b36b676392740c486c99a61decace1b9
|
|
4
|
+
data.tar.gz: 3ba3d937aa02559bdba4d7270a9066bb76e02d9950ae6d2f754a95b2c809b26b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 71c2c459d4655436018b6785ae599b543df3e2df636580e0283775425bf6222609e4930740d8f05e3fc10ed078d54ffddca45cc9c9a55546920e4500ddd67418
|
|
7
|
+
data.tar.gz: 96da9117823ab068a59af2bd18f259974adea66cf72f71d853443003aa1bcc27771a2eba3a58ee8da0c3a3c024d0d3809550a6cc0eafd2a26561cce1d4f8c419
|
data/lib/eman_fraction.rb
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
class Fraction < Numeric
|
|
2
2
|
attr_reader :numerator, :denominator
|
|
3
3
|
def initialize(numerator:, denominator:)
|
|
4
|
+
raise InvalidFraction, 'denominator cannot be zero' if denominator.zero?
|
|
5
|
+
|
|
4
6
|
@numerator = numerator
|
|
5
7
|
@denominator = denominator
|
|
6
8
|
simplify
|
|
@@ -8,9 +10,26 @@ class Fraction < Numeric
|
|
|
8
10
|
|
|
9
11
|
def +(other)
|
|
10
12
|
return 0 if zeros?(other) # Don't really know how to deal with this otherwise
|
|
13
|
+
|
|
11
14
|
self_numerator = numerator * other.denominator
|
|
12
15
|
other_numerator = other.numerator * denominator
|
|
13
|
-
self.class.new(numerator: self_numerator + other_numerator, denominator: other.denominator * denominator)
|
|
16
|
+
new_fraction = self.class.new(numerator: self_numerator + other_numerator, denominator: other.denominator * denominator)
|
|
17
|
+
|
|
18
|
+
return 0 if new_fraction.numerator.zero?
|
|
19
|
+
|
|
20
|
+
new_fraction
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def -(other)
|
|
24
|
+
self_numerator = numerator * other.denominator
|
|
25
|
+
other_numerator = other.numerator * denominator
|
|
26
|
+
new_fraction = self.class.new(
|
|
27
|
+
numerator: self_numerator - other_numerator,
|
|
28
|
+
denominator: other.denominator * denominator
|
|
29
|
+
)
|
|
30
|
+
return 0 if new_fraction.numerator.zero?
|
|
31
|
+
|
|
32
|
+
new_fraction
|
|
14
33
|
end
|
|
15
34
|
|
|
16
35
|
def ==(other)
|
|
@@ -26,16 +45,19 @@ class Fraction < Numeric
|
|
|
26
45
|
num_factors = factors(numerator)
|
|
27
46
|
common_factors = denon_factors & num_factors
|
|
28
47
|
return if common_factors.empty?
|
|
29
|
-
|
|
30
|
-
@
|
|
48
|
+
|
|
49
|
+
@numerator /= common_factors.last
|
|
50
|
+
@denominator /= common_factors.last
|
|
31
51
|
end
|
|
32
52
|
|
|
33
53
|
def factors(number)
|
|
34
|
-
(1..number).select {|x| number % x == 0 }
|
|
54
|
+
(1..number).select { |x| number % x == 0 }
|
|
35
55
|
end
|
|
36
56
|
|
|
37
57
|
def zeros?(other)
|
|
38
|
-
(other.zero? || (other.numerator.zero? && other.denominator.zero?)) &&
|
|
39
|
-
|
|
58
|
+
(other.zero? || (other.numerator.zero? && other.denominator.zero?)) &&
|
|
59
|
+
(numerator.zero? && denominator.zero?)
|
|
60
|
+
end
|
|
61
|
+
class InvalidFraction < StandardError
|
|
40
62
|
end
|
|
41
|
-
end
|
|
63
|
+
end
|