# bhuga/quantity

More rational fixes

1 parent 8f9c041 commit fd597ef4c3ff7ad3a58ebe9c960842f7f0e79064 committed Jan 5, 2010
Showing with 14 additions and 1 deletion.
1. +5 −1 lib/quantity.rb
2. +6 −0 lib/quantity/unit.rb
3. +2 −0 spec/quantity.spec
4. +1 −0 spec/unit.spec
 @@ -119,6 +119,8 @@ def coerce(other) [Quantity.new(other, @unit),self] elsif defined?(Rational) && (@value.is_a?(Fixnum)) && (other.is_a?(Fixnum)) [Quantity.new(Rational(other), @unit), self] + elsif defined?(Rational) && (other.is_a?(Rational)) + [Quantity.new(other, @unit), self] else [Quantity.new(other.to_f, @unit),Quantity.new(@value.to_f, @unit)] end @@ -196,8 +198,10 @@ def /(other) ref = nil if defined?(Rational) && (@value.is_a?(Fixnum)) && (other.is_a?(Fixnum)) ref = Rational(@reference_value,other.reference_value) - else + elsif defined?(Rational) && (@value.is_a?(Rational)) && (other.is_a?(Rational)) ref = @reference_value / other.reference_value + else + ref = @reference_value / other.reference_value.to_f end Quantity.new({:unit => @unit / other.unit, :reference_value => ref}) else
 @@ -115,6 +115,10 @@ def convert_proc(to) lambda do | from | from * Rational(@value, to.value) end + elsif defined?(Rational) && (@value.is_a?(Rational)) && (to.value.is_a?(Rational)) + lambda do | from | + from * @value / to.value + end else lambda do | from | from * (@value / to.value.to_f) @@ -130,6 +134,8 @@ def convert_proc(to) # @return [Numeric] def value_for(reference_value) if defined?(Rational) && (reference_value.is_a?(Fixnum)) && (@value.is_a?(Fixnum)) + Rational(reference_value, @value) + elsif defined?(Rational) && (reference_value.is_a?(Rational) || reference_value.is_a?(Fixnum)) && (@value.is_a?(Rational)) reference_value / @value #Rational(reference_value, @value) else reference_value / @value.to_f
 @@ -3,6 +3,7 @@ \$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), 'lib'))) require 'quantity' require 'quantity/systems/si' require 'quantity/systems/us' +require 'rational' describe Quantity do @@ -29,6 +30,7 @@ describe Quantity do 1.meter.in_centimeters.should == 100 50.centimeters.to_meters.should == 0.5 10.meters.convert(:feet).should be_close 32.808399.feet, 10**-6 + 1.mm.to_m.should == 0.001 end it "should convert from one type to another when not using the reference" do
 @@ -2,6 +2,7 @@ \$:.unshift(File.expand_path(File.join(File.dirname(__FILE__), 'lib'))) require 'quantity/dimension' require 'quantity/unit' +require 'rational' describe Quantity::Unit do