Module: Unity::Arithmetic

Extended by:
ActiveSupport::Concern
Includes:
Conversion
Included in:
Quantity
Defined in:
lib/unity/arithmetic.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from Conversion

#convert_to

Methods included from Fraction

#expanded_denominator, #expanded_numerator, #expression, #expression=, #initialize, #inverse, #reduce, #unit

Methods included from Comparison

#==, #compatible!, #compatible?

Methods included from Dimension::Integer

#dimension_int, #property, #property_name

Instance Method Details

#*(other) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/unity/arithmetic.rb', line 37

def * other
  other = ensure_not_numeric other
  other = other.convert_to(self) if self.compatible? other
  new_numerator = self.numerator + other.numerator
  new_denominator = self.denominator + other.denominator
  self.class.new(:value => (self.value * other.value), :numerator => new_numerator, :denominator => new_denominator).tap(&:reduce)
end

#+(other) ⇒ Object



16
17
18
19
20
21
# File 'lib/unity/arithmetic.rb', line 16

def + other
  other = ensure_not_numeric other
  self.compatible! other
  other = other.convert_to(self) 
  self.class.new(:value => (self.value + other.value), :numerator => self.numerator, :denominator => self.denominator)
end

#-(other) ⇒ Object



24
25
26
27
28
29
# File 'lib/unity/arithmetic.rb', line 24

def - other
  other = ensure_not_numeric other
  self.compatible! other
  other = other.convert_to(self) 
  self.class.new(:value => (self.value - other.value), :numerator => self.numerator, :denominator => self.denominator)
end

#/(other) ⇒ Object



31
32
33
34
35
# File 'lib/unity/arithmetic.rb', line 31

def / other
  other = ensure_not_numeric other
  other = other.convert_to(self) if self.compatible? other
  self * other.inverse
end

#coerce(other) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/unity/arithmetic.rb', line 45

def coerce(other)
  case other
  when Numeric
    return convert_numeric(other), self
  else
    raise TypeError, "#{self.class} can't be coerced into #{other.class}"
  end
end