Class: Reality::Measure

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/reality/measure.rb,
lib/reality/measure/unit.rb

Overview

Wrapper for numeric values. Example: Reality::Entity.new('Ukraine', load: true).area => # Keeps information about value unit Allows coercion and general Numeric operations

Defined Under Namespace

Classes: Unit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, unit) ⇒ Measure

Returns a new instance of Measure.

Parameters:

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


19
20
21
# File 'lib/reality/measure.rb', line 19

def initialize(amount, unit)
  @amount, @unit = Rational(amount), Unit.parse(unit)
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



11
12
13
# File 'lib/reality/measure.rb', line 11

def amount
  @amount
end

#unitObject (readonly)

Returns the value of attribute unit.



11
12
13
# File 'lib/reality/measure.rb', line 11

def unit
  @unit
end

Class Method Details

.coerce(amount, unit) ⇒ Object



13
14
15
# File 'lib/reality/measure.rb', line 13

def Measure.coerce(amount, unit)
  amount && unit && new(amount, unit)
end

Instance Method Details

#*(other) ⇒ Object



43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



68
69
70
# File 'lib/reality/measure.rb', line 68

def **(num)
  (num-1).times.inject(self){|res| res*self}
end

#+(other) ⇒ Object



33
34
35
36
37
# File 'lib/reality/measure.rb', line 33

def +(other)
  check_compatibility!(other)

  self.class.new(amount + other.amount, unit)
end

#-(other) ⇒ Object



39
40
41
# File 'lib/reality/measure.rb', line 39

def -(other)
  self + (-other)
end

#-@Object



29
30
31
# File 'lib/reality/measure.rb', line 29

def -@
  self.class.new(-amount, unit)
end

#/(other) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# 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) ⇒ Object



23
24
25
26
27
# File 'lib/reality/measure.rb', line 23

def <=>(other)
  check_compatibility!(other)
  
  amount <=> other.amount
end

#absObject



72
73
74
# File 'lib/reality/measure.rb', line 72

def abs
  self.class.new(amount.abs, unit)
end

#inspectObject



94
95
96
# File 'lib/reality/measure.rb', line 94

def inspect
  "#<%s(%s %s)>" % [self.class, Util::Format.number(amount), unit]
end

#to_fObject



86
87
88
# File 'lib/reality/measure.rb', line 86

def to_f
  amount.to_f
end

#to_hObject



82
83
84
# File 'lib/reality/measure.rb', line 82

def to_h
  {amount: amount.to_f, unit: unit.to_s}
end

#to_iObject



90
91
92
# File 'lib/reality/measure.rb', line 90

def to_i
  amount.to_i
end

#to_sObject



78
79
80
# File 'lib/reality/measure.rb', line 78

def to_s
  '%s%s' % [Util::Format.number(amount), unit]
end