def to(other)
return self if other.nil?
return self if TrueClass === other
return self if FalseClass === other
if (Unit === other && other.is_temperature?) || (String === other && other =~ /temp(K|C|R|F)/)
raise ArgumentError, "Receiver is not a temperature unit" unless self.signature == 400
start_unit = self.units
target_unit = other.units rescue other
unless @base_scalar
@base_scalar = case start_unit
when 'tempC' : @scalar + 273.15
when 'tempK' : @scalar
when 'tempF' : (@scalar+459.67)*(5.0/9.0)
when 'tempR' : @scalar*(5.0/9.0)
end
end
q= case target_unit
when 'tempC' : @base_scalar - 273.15
when 'tempK' : @base_scalar
when 'tempF' : @base_scalar * (9.0/5.0) - 459.67
when 'tempR' : @base_scalar * (9.0/5.0)
end
??
Unit.new("#{q} #{target_unit}")
else
case other
when Unit:
return self if other.units == self.units
target = other
when String: target = Unit.new(other)
else
raise ArgumentError, "Unknown target units"
end
raise ArgumentError, "Incompatible Units" unless self =~ target
one = @numerator.map {|x| @@PREFIX_VALUES[x] ? @@PREFIX_VALUES[x] : x}.map {|i| i.kind_of?(Numeric) ? i : @@UNIT_VALUES[i][:scalar] }.compact
two = @denominator.map {|x| @@PREFIX_VALUES[x] ? @@PREFIX_VALUES[x] : x}.map {|i| i.kind_of?(Numeric) ? i : @@UNIT_VALUES[i][:scalar] }.compact
v = one.inject(1) {|product,n| product*n} / two.inject(1) {|product,n| product*n}
one = target.numerator.map {|x| @@PREFIX_VALUES[x] ? @@PREFIX_VALUES[x] : x}.map {|x| x.kind_of?(Numeric) ? x : @@UNIT_VALUES[x][:scalar] }.compact
two = target.denominator.map {|x| @@PREFIX_VALUES[x] ? @@PREFIX_VALUES[x] : x}.map {|x| x.kind_of?(Numeric) ? x : @@UNIT_VALUES[x][:scalar] }.compact
y = one.inject(1) {|product,n| product*n} / two.inject(1) {|product,n| product*n}
q = @scalar * v/y
Unit.new(:scalar=>q, :numerator=>target.numerator, :denominator=>target.denominator, :signature => target.signature)
end
end