Class: Float

Inherits:
Object show all
Defined in:
lib/units/standard.rb,
lib/units/base.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#*(other) ⇒ Object

CURRENTLY: Scalar multiplication (a number with a unit to a number without a unit) TO COME: Non-scalar multiplication This will require keeping track of the exponents, like:

meters is [:meters, 1], square inches is [:inches, 2], cubic mililiters is [:milileters, 3]

And then we can get rid of the silly :m3's in the volume conversion as well as add new units like:

:joules => [[:kilograms, 1], [:meters, 1], [:seconds, -2]]

120
121
122
123
124
125
126
127
128
129
130
# File 'lib/units/base.rb', line 120

def *(other)
  if Numeric === other && kind && other.kind
    raise UnitsError, "currently cannot mutiply two numers with units, try scalar multiplication instead"

  elsif Numeric === other && kind.nil? && other.kind.nil?
    multiply other

  else
    multiply_with_units other
  end
end

#+(other) ⇒ Object

Add only numbers that both have units or both don't have units


97
98
99
100
101
102
103
104
105
106
107
# File 'lib/units/base.rb', line 97

def +(other)
  if Float === other && kind && other.kind
    add_with_units( unit == other.unit ? other : other.send(unit) )

  elsif Numeric === other && (kind || other.kind) && (kind.nil? || other.kind.nil?)
    raise UnitsError, "cannot add a number without units to one with units"

  else
    add other
  end
end

#_to_iObject


134
# File 'lib/units/standard.rb', line 134

alias :_to_i :to_i

#_to_intObject


144
# File 'lib/units/standard.rb', line 144

alias :_to_int :to_int

#addObject


95
# File 'lib/units/base.rb', line 95

alias :add :+

#add_with_units(other) ⇒ Object


108
109
110
# File 'lib/units/base.rb', line 108

def add_with_units(other)
  add(other).send(unit)
end

#multiplyObject


112
# File 'lib/units/base.rb', line 112

alias :multiply :*

#multiply_with_units(other) ⇒ Object


131
132
133
# File 'lib/units/base.rb', line 131

def multiply_with_units(other)
  multiply(other).send(unit || other.unit)
end

#to_iObject


135
136
137
138
139
140
141
142
# File 'lib/units/standard.rb', line 135

def to_i
  case kind
  when :time
    to_seconds._to_i
  when :size
    to_bytes._to_i
  end
end

#to_intObject


145
146
147
148
149
150
151
152
# File 'lib/units/standard.rb', line 145

def to_int
  case kind
  when :time
    to_seconds._to_int
  when :size
    to_bytes._to_int
  end
end