193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
# File 'lib/units/measure.rb', line 193
def in(other, mode=:absolute)
other = Units.units(other) if other.kind_of?(String)
other = Measure.new(1.0, other) unless other.kind_of?(Measure)
other = other.base
this = self.base
dims = this.units.keys | other.units.keys
mag = this.magnitude/other.magnitude
dims.each do |dim|
if !this.units[dim] || !other.units[dim] ||
(this.units[dim].last != other.units[dim].last)
raise "Inconsistent units #{Units.units_descr(this.units)} #{Units.units_descr(other.units)}"
end
this_u, mult = this.units[dim]
other_u = other.units[dim].first
mag *= Units.conversion_factor(this_u, other_u)**mult
end
if mode!=:relative && dims.size==1 && this.units[dims.first].last==1
mag += Units.conversion_bias(this.units[dims.first].first, other.units[dims.first].first)
end
mag
end
|