diff --git a/lib/unit/base.rb b/lib/unit/base.rb index 50d3dc4..0447e05 100644 --- a/lib/unit/base.rb +++ b/lib/unit/base.rb @@ -31,13 +31,7 @@ def +(other) if @uom == other.uom self.class.new((scalar + other.scalar), @uom, @components + other.components) else - if self < other - scaled_other = other.convert_to(self.uom) - self.class.new((@scalar + scaled_other.scalar), scaled_other.uom, @components + other.components) - else - scaled_self = self.convert_to(other.uom) - self.class.new((scaled_self.scalar + other.scalar), scaled_self.uom, @components + other.components) - end + use_operator_on_other(:+, other) end end @@ -45,13 +39,7 @@ def -(other) if @uom == other.uom self.class.new((@scalar - other.scalar), @uom, @components + other.components) else - if self < other - scaled_other = other.convert_to(self.uom) - self.class.new((@scalar - scaled_other.scalar), scaled_other.uom, @components + other.components) - else - scaled_self = self.convert_to(other.uom) - self.class.new((scaled_self.scalar - other.scalar), scaled_self.uom, @components + other.components) - end + use_operator_on_other(:-, other) end end @@ -147,6 +135,16 @@ def concentration? false end - #Display methods + private + + def use_operator_on_other(operator, other) + if self < other + scaled_other = other.convert_to(self.uom) + self.class.new((@scalar.send(operator, scaled_other.scalar)), scaled_other.uom, @components + other.components) + else + scaled_self = self.convert_to(other.uom) + self.class.new((scaled_self.scalar.send(operator, other.scalar)), scaled_self.uom, @components + other.components) + end + end end end diff --git a/lib/unit/concentration.rb b/lib/unit/concentration.rb index 5572fee..ec69cd5 100644 --- a/lib/unit/concentration.rb +++ b/lib/unit/concentration.rb @@ -16,32 +16,11 @@ def initialize(numerator, denominator) end def +(other) - if other.is_a? Concentration - #Add numerators - con1, con2 = Concentration.equivalise(self, other) - Concentration.new(con1.numerator + con2.numerator, - con1.denominator #This is the same for both cons because of the equivalise method - ) - elsif other.is_a? Mass - Concentration.new(self.numerator + other, self.denominator) - elsif other.is_a? Volume - #Making the assumption that you're diluting - Concentration.new(self.numerator, self.denominator + other) - end + use_operator_on_other(:+, other) end def -(other) - if other.is_a? Concentration - con1, con2 = Concentration.equivalise(self, other) - Concentration.new(con1.numerator - con2.numerator, - con1.denominator #This is the same for both cons because of the equivalise method - ) - elsif other.is_a? Mass - Concentration.new(self.numerator - other, self.denominator) - elsif other.is_a? Volume - #Making the assumption that you're diluting - Concentration.new(self.numerator, self.denominator - other) - end + use_operator_on_other(:-, other) end def /(other) @@ -113,6 +92,21 @@ def unit? private + def use_operator_on_other(operator, other) + if other.is_a? Concentration + #Add numerators + con1, con2 = Concentration.equivalise(self, other) + Concentration.new(con1.numerator.send(operator, con2.numerator), + con1.denominator #This is the same for both cons because of the equivalise method + ) + elsif other.is_a? Mass + Concentration.new(self.numerator.send(operator, other), self.denominator) + elsif other.is_a? Volume + #Making the assumption that you're diluting + Concentration.new(self.numerator, self.denominator.send(operator, other)) + end + end + def self.equivalise(con1, con2) #if equivalent denoms, don't need to convert if con1.denominator == con2.denominator diff --git a/lib/unit/unit.rb b/lib/unit/unit.rb index 7715520..59c6db4 100644 --- a/lib/unit/unit.rb +++ b/lib/unit/unit.rb @@ -11,23 +11,17 @@ def self.scale_hash #We need to make sure we can't add different unitless objects together def +(other) - if self.uom != other.uom && other.is_a?(Unit) - raise IncompatibleUnitsError.new("You cannot add two different unitless units together") - end + check_compatibility("add", other) super(other) end def -(other) - if self.uom != other.uom && other.is_a?(Unit) - raise IncompatibleUnitsError.new("You cannot subtract two different unitless units together") - end + check_compatibility("subtract", other) super(other) end def /(other) - if self.uom != other.uom && other.is_a?(Unit) - raise IncompatibleUnitsError.new("You cannot divide two different unitless units together") - end + check_compatibility("divide", other) if other.is_a?(Volume) Concentration.new(self, other) else @@ -36,15 +30,13 @@ def /(other) end def <=>(other) - if self.uom != other.uom && other.is_a?(Unit) - raise IncompatibleUnitsError.new("You cannot compare two different unitless units together") - end + check_compatibility("compare", other) super(other) end def convert_to(uom) if self.uom != uom - raise IncompatibleUnitsError.new("You cannot compare two different unitless units together") + raise_incompatible_error("compare") end super(uom) end @@ -52,9 +44,21 @@ def convert_to(uom) def self.equivalise(u1, u2) if u1.uom != u2.uom && other.is_a?(Unit) - raise IncompatibleUnitsError.new("You cannot compare two different unitless units together") + raise_incompatible_error("compare") end super(u1, u2) end + + private + + def check_compatibility(operation, other) + if self.uom != other.uom && other.is_a?(Unit) + raise_incompatible_error(operation) + end + end + + def raise_incompatible_error(operation) + raise IncompatibleUnitsError.new("You cannot #{operation} two different unitless units together") + end end end