class SimpleUnits::Unit

Attributes

composite[RW]
value[RW]

Public Class Methods

convert_dimension(to_unit, from_unit) click to toggle source
# File lib/simple_units/unit.rb, line 6
def convert_dimension(to_unit, from_unit)
  throw :CannotConvertDimension
end
new(value, units) click to toggle source
# 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
simplify_dimensions(unit) click to toggle source

Not really implemented yet

# File lib/simple_units/unit.rb, line 11
def simplify_dimensions(unit)
  unit
end

Public Instance Methods

*(other) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/simple_units/unit.rb, line 57
def -(other)
  self + other.opposite
end
/(other) click to toggle source
# File lib/simple_units/unit.rb, line 75
def /(other)
  self * other.inverse
end
inspect() click to toggle source
# 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
inverse() click to toggle source
# File lib/simple_units/unit.rb, line 83
def inverse
  self.class.new(1 / value.to_f, composite.inverse)
end
opposite() click to toggle source
# File lib/simple_units/unit.rb, line 79
def opposite
  self.class.new(-value, composite)
end
same_dimension?(other) click to toggle source
# File lib/simple_units/unit.rb, line 99
def same_dimension?(other)
  self.composite == other.composite
end
to_f() click to toggle source
# File lib/simple_units/unit.rb, line 41
def to_f
  to_something("f")
end
to_i() click to toggle source
# File lib/simple_units/unit.rb, line 37
def to_i
  to_something("i")
end
to_s() click to toggle source
# File lib/simple_units/unit.rb, line 95
def to_s; inspect; end
to_something(thing) click to toggle source
# File lib/simple_units/unit.rb, line 45
def to_something(thing)
  value.send("to_#{thing}")
end
to_str() click to toggle source
# File lib/simple_units/unit.rb, line 97
def to_str; to_s; end
units() click to toggle source
# File lib/simple_units/unit.rb, line 33
def units
  composite.to_s
end