Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More rational fixes

  • Loading branch information...
commit fd597ef4c3ff7ad3a58ebe9c960842f7f0e79064 1 parent 8f9c041
@bhuga authored
View
6 lib/quantity.rb
@@ -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
View
6 lib/quantity/unit.rb
@@ -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
View
2  spec/quantity.spec
@@ -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
View
1  spec/unit.spec
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.