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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/eman_fraction.rb +29 -7
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 38e53b504f69bd6698569dd50107b2e6dd42b64c13112a8d7601deb19ad1a828
4
- data.tar.gz: 4c969e17cc77bc7a20cbe6ab04d9bf850512def29ab9f355ea083bf4fc29b3d7
3
+ metadata.gz: 965800579a72dbb17f7b371c12b1b8e1b36b676392740c486c99a61decace1b9
4
+ data.tar.gz: 3ba3d937aa02559bdba4d7270a9066bb76e02d9950ae6d2f754a95b2c809b26b
5
5
  SHA512:
6
- metadata.gz: 89f7d093224fe65b7ce7689843a46191a317bb902b2c2af337561c8a2c60534bba78dc116166df43ae3cc32f0687cb35b999f8435dc80cd28ceed8f3cbc19649
7
- data.tar.gz: bd80e28a8dc5699e5f38712b97453a654ad9f84fcdfc8ec16a0479bce8f225b8f7739403c707441d93d363158b5625ed2caf029e6d7926bcb9ce56a01a62c659
6
+ metadata.gz: 71c2c459d4655436018b6785ae599b543df3e2df636580e0283775425bf6222609e4930740d8f05e3fc10ed078d54ffddca45cc9c9a55546920e4500ddd67418
7
+ data.tar.gz: 96da9117823ab068a59af2bd18f259974adea66cf72f71d853443003aa1bcc27771a2eba3a58ee8da0c3a3c024d0d3809550a6cc0eafd2a26561cce1d4f8c419
@@ -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
- @numerator = @numerator / common_factors.last
30
- @denominator = @denominator / common_factors.last
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
- (self.numerator.zero? && self.denominator.zero?)
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Eman_Fraction
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emmanuel Genard