Class: Reality::Measure
- Inherits:
-
Object
- Object
- Reality::Measure
- 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
=> #
Defined Under Namespace
Classes: Unit
Instance Attribute Summary collapse
-
#amount ⇒ Object
readonly
Returns the value of attribute amount.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
Class Method Summary collapse
Instance Method Summary collapse
- #*(other) ⇒ Object
- #**(num) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(other) ⇒ Object
- #<=>(other) ⇒ Object
- #abs ⇒ Object
-
#initialize(amount, unit) ⇒ Measure
constructor
A new instance of Measure.
- #inspect ⇒ Object
- #to_f ⇒ Object
- #to_h ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
Constructor Details
Instance Attribute Details
#amount ⇒ Object (readonly)
Returns the value of attribute amount.
11 12 13 |
# File 'lib/reality/measure.rb', line 11 def amount @amount end |
#unit ⇒ Object (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 |
#abs ⇒ Object
72 73 74 |
# File 'lib/reality/measure.rb', line 72 def abs self.class.new(amount.abs, unit) end |
#inspect ⇒ Object
94 95 96 |
# File 'lib/reality/measure.rb', line 94 def inspect "#<%s(%s %s)>" % [self.class, Util::Format.number(amount), unit] end |
#to_f ⇒ Object
86 87 88 |
# File 'lib/reality/measure.rb', line 86 def to_f amount.to_f end |
#to_h ⇒ Object
82 83 84 |
# File 'lib/reality/measure.rb', line 82 def to_h {amount: amount.to_f, unit: unit.to_s} end |
#to_i ⇒ Object
90 91 92 |
# File 'lib/reality/measure.rb', line 90 def to_i amount.to_i end |
#to_s ⇒ Object
78 79 80 |
# File 'lib/reality/measure.rb', line 78 def to_s '%s%s' % [Util::Format.number(amount), unit] end |