class Reality::Measure

Wrapper for numeric values. Example:

Reality::Entity.new('Ukraine', load: true).area
 => #<Reality::Measure(603,500 km²)>

Keeps information about value unit Allows coercion and general Numeric operations

Attributes

amount[R]
unit[R]

Public Class Methods

coerce(amount, unit) click to toggle source
# File lib/reality/measure.rb, line 13
def Measure.coerce(amount, unit)
  amount && unit && new(amount, unit)
end
new(amount, unit) click to toggle source

@param amount - numeric value, e.g. 100.5 @param unit - can be any string, e.g. 'km', '$'

# File lib/reality/measure.rb, line 19
def initialize(amount, unit)
  @amount, @unit = Rational(amount), Unit.parse(unit)
end

Public Instance Methods

*(other) click to toggle source
# File lib/reality/measure.rb, line 43
def *(other)
  case other
  when Numeric
    self.class.new(amount * other, unit)
  when self.class
    self.class.new(amount * other.amount, unit * other.unit)
  else
    fail ArgumentError, "Can't multiply by #{other.class}"
  end
end
**(num) click to toggle source
# File lib/reality/measure.rb, line 68
def **(num)
  (num-1).times.inject(self){|res| res*self}
end
+(other) click to toggle source
# File lib/reality/measure.rb, line 33
def +(other)
  check_compatibility!(other)

  self.class.new(amount + other.amount, unit)
end
-(other) click to toggle source
# File lib/reality/measure.rb, line 39
def -(other)
  self + (-other)
end
-@() click to toggle source
# File lib/reality/measure.rb, line 29
def -@
  self.class.new(-amount, unit)
end
/(other) click to toggle source
# File lib/reality/measure.rb, line 54
def /(other)
  case other
  when Numeric
    self.class.new(amount / other, unit)
  when self.class
    un = unit / other.unit
    un.scalar? ?
      amount / other.amount :
      self.class.new(amount / other.amount, un)
  else
    fail ArgumentError, "Can't divide by #{other.class}"
  end
end
<=>(other) click to toggle source
# File lib/reality/measure.rb, line 23
def <=>(other)
  check_compatibility!(other)
  
  amount <=> other.amount
end
abs() click to toggle source
# File lib/reality/measure.rb, line 72
def abs
  self.class.new(amount.abs, unit)
end
inspect() click to toggle source
# File lib/reality/measure.rb, line 94
def inspect
  "#<%s(%s %s)>" % [self.class, Util::Format.number(amount), unit]
end
to_f() click to toggle source
# File lib/reality/measure.rb, line 86
def to_f
  amount.to_f
end
to_h() click to toggle source
# File lib/reality/measure.rb, line 82
def to_h
  {amount: amount.to_f, unit: unit.to_s}
end
to_i() click to toggle source
# File lib/reality/measure.rb, line 90
def to_i
  amount.to_i
end
to_s() click to toggle source
# File lib/reality/measure.rb, line 78
def to_s
  '%s%s' % [Util::Format.number(amount), unit]
end

Private Instance Methods

check_compatibility!(other) click to toggle source
# File lib/reality/measure.rb, line 100
def check_compatibility!(other)
  unless other.kind_of?(self.class) && other.unit == unit
    fail ArgumentError, "#{self} incompatible with #{other}"
  end
end