Class: SimpleUnits::Unit

Inherits:
Numeric
  • Object
show all
Defined in:
lib/simple_units/unit.rb

Defined Under Namespace

Classes: MismatchError, UnknownUnitError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, units) ⇒ Unit

Returns a new instance of Unit.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/simple_units/unit.rb', line 18

def initialize(value, units)
  @value = value
  case units.class.to_s
  when "String", "Symbol"
    @composite = SimpleUnits::Composite.from_string units.to_s
  when "SimpleUnits::Context"
    @composite = SimpleUnits::Composite.from_context units
  when "SimpleUnits::Composite"
    @composite = units
  else
    raise UnknownUnitError.new("#{units} is a #{units.class}")
  end
  
end

Instance Attribute Details

#compositeObject

Returns the value of attribute composite.



16
17
18
# File 'lib/simple_units/unit.rb', line 16

def composite
  @composite
end

#valueObject

Returns the value of attribute value.



16
17
18
# File 'lib/simple_units/unit.rb', line 16

def value
  @value
end

Class Method Details

.convert_dimension(to_unit, from_unit) ⇒ Object



6
7
8
# File 'lib/simple_units/unit.rb', line 6

def convert_dimension(to_unit, from_unit)
  throw :CannotConvertDimension
end

.simplify_dimensions(unit) ⇒ Object

Not really implemented yet



11
12
13
# File 'lib/simple_units/unit.rb', line 11

def simplify_dimensions(unit)
  unit
end

Instance Method Details

#*(other) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/simple_units/unit.rb', line 61

def *(other)
  case other.class.to_s
  when "Fixnum", "Float", "Integer", "Numeric"
    new_value = value * other
    new_composite = composite
  when "SimpleUnits::Unit"
    new_value = value * other.value
    new_composite = composite * other.composite  
  else
    raise MismatchError.new "#{other} is a #{other.class.to_s}"
  end
  self.class.simplify_dimensions self.class.new(new_value, new_composite)
end

#+(other) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/simple_units/unit.rb', line 49

def +(other)
  if same_dimension? other
    self.class.new(value + other.value, composite)
  else
    self + SimpleUnits::Unit.convert_dimension(self, other)
  end
end

#-(other) ⇒ Object



57
58
59
# File 'lib/simple_units/unit.rb', line 57

def -(other)
  self + other.opposite
end

#/(other) ⇒ Object



75
76
77
# File 'lib/simple_units/unit.rb', line 75

def /(other)
  self * other.inverse
end

#inspectObject



87
88
89
90
91
92
93
# File 'lib/simple_units/unit.rb', line 87

def inspect
  if composite.inspector.nil?
    "#{value} #{composite.to_s}"
  else
    composite.inspector.call(value)
  end
end

#inverseObject



83
84
85
# File 'lib/simple_units/unit.rb', line 83

def inverse
  self.class.new(1 / value.to_f, composite.inverse)
end

#oppositeObject



79
80
81
# File 'lib/simple_units/unit.rb', line 79

def opposite
  self.class.new(-value, composite)
end

#same_dimension?(other) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/simple_units/unit.rb', line 99

def same_dimension?(other)
  self.composite == other.composite
end

#to_fObject



41
42
43
# File 'lib/simple_units/unit.rb', line 41

def to_f
  to_something("f")
end

#to_iObject



37
38
39
# File 'lib/simple_units/unit.rb', line 37

def to_i
  to_something("i")
end

#to_sObject



95
# File 'lib/simple_units/unit.rb', line 95

def to_s; inspect; end

#to_something(thing) ⇒ Object



45
46
47
# File 'lib/simple_units/unit.rb', line 45

def to_something(thing)
  value.send("to_#{thing}")
end

#to_strObject



97
# File 'lib/simple_units/unit.rb', line 97

def to_str; to_s; end

#unitsObject



33
34
35
# File 'lib/simple_units/unit.rb', line 33

def units
  composite.to_s
end