Skip to content
2 changes: 1 addition & 1 deletion lib/quantity/dimension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def self.add_dimension(name, *aliases)
dim = name
name.name = aliases.first if aliases.first
else
dim = self.for(name) || self.new({ :name => aliases.first , :description => name})
dim = self.for(name) || self.new({ :name => name , :description => name})
self.add_alias(dim,name)
end
unless (dim.class == Dimension)
Expand Down
4 changes: 2 additions & 2 deletions lib/quantity/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def initialize(opts)
unless opts[:name] || !@dimension.is_base?
raise ArgumentError, "Single-order units must be uniquely named (#{name} - #{dimension})"
end
@name = opts[:name] || string_form
@name = (opts[:name] || string_form).to_sym
self.class.add_alias(self,@name.to_sym)
raise ArgumentError, "Creating new unit with no value" unless @value
end
Expand Down Expand Up @@ -331,7 +331,7 @@ def string_form
def self.string_form(dimension,units)
string = dimension.string_form
units.each do | dimension, unit |
string = string.gsub(dimension.name.to_s, unit.name.to_s)
string = string.gsub(dimension.string_form, unit.name.to_s)
end
string
end
Expand Down
12 changes: 9 additions & 3 deletions spec/dimension.spec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe Quantity::Dimension do
end

it "creates complex dimensions" do
Quantity::Dimension.add_dimension :'length^2', :area
Quantity::Dimension.add_dimension :area, :'length^2'
area = Quantity::Dimension.for(:area)
area.to_s.should == "area"
area.name.should == :area
Expand Down Expand Up @@ -92,28 +92,34 @@ describe Quantity::Dimension do
end

it "provides a hash form for simple dimensions" do
@length.hash.should == { :length => 1 }
pending
@length.to_hash.should == { :length => 1 }
end

it "provides a hash form for unnamed dimensions" do
pending
lm = @length * @mass
lm.hash.should == { :length => 1, :mass => 1 }
lm.to_hash.should == { :length => 1, :mass => 1 }
end

it "provides a hash form for named dimensions" do
pending
@area.to_hash.should == { :area => 1 }
end

it "provides a reduced hash form" do
pending
@length.to_hash.should == @length.reduced_hash
@area.reduced.to_hash.should == { :length => 2 }
end

it "provides a hash form with negative components" do
pending
@force.to_hash.should = { :length => 1, :mass => -1 , :time => -2 }
end

it "provides a reduced form" do
pending
@area.name.should == :area
@area.reduce.name.should == :'length^2'
end
Expand Down
8 changes: 4 additions & 4 deletions spec/quantity.spec
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe Quantity do

it "should have a string representation" do
2.meters.to_s.should == "2 meter"
(2.meters * 2.meters).to_s.should == (defined?(Rational) ? "4 meter^2" : "4.0 meter^2")
(2.meters * 2.meters).to_s.should == (defined?(Rational) ? "#{4.to_r.to_s} meter^2" : "4.0 meter^2")
end

end
Expand All @@ -55,7 +55,7 @@ describe Quantity do
it "converts derived units to named units" do
(1.centimeter * 1.centimeter * 1.centimeter).should == 0.1.centiliter
(1000.mm * 1.mm * 1.mm).should == 1.ml
(1.mm**3).unit.name.should == 'millimeter^3'
(1.mm**3).unit.name.should == :'millimeter^3'
(1.mm**3).measures.name.should == :volume
(1.centimeter * 1.centimeter).measures.name.should == :area
(30.meters / 1.second).measures.name.should == :speed
Expand Down Expand Up @@ -169,7 +169,7 @@ describe Quantity do
end

it "does not add items of different types" do
lambda { 12.meters + 24.picograms }.should raise_error TypeError
lambda { 12.meters + 24.picograms }.should raise_error ArgumentError
end

it "adds negative quantities" do
Expand All @@ -185,7 +185,7 @@ describe Quantity do
end

it "does not add items of different types" do
lambda { (12.meters - 3650.picograms)}.should raise_error TypeError
lambda { (12.meters - 3650.picograms)}.should raise_error ArgumentError
end

it "subtracts numerics from quantities" do
Expand Down
10 changes: 5 additions & 5 deletions spec/unit.spec
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe Quantity::Unit do
it "supports units of different dimensions" do
s_f3 = @second * (@foot * @foot * @foot)
s_f3.name.should == :'foot^3*second'
s_f3.value.should == @foot.value**3 / @second.value
s_f3.value.should == @foot.value**3 * @second.value
end

it "supports different units of the same dimension" do
Expand Down Expand Up @@ -222,7 +222,7 @@ describe Quantity::Unit do
it "supports mixed unit divisors" do
result = @meter / (@gram * @second)
result.name.should == :'meter/gram*second'
result.value.should == @meter.value / (@gram.value*@second.value)
result.value.should == @meter.value.to_r / (@gram.value*@second.value).to_r
end

it "simplifies results" do
Expand Down Expand Up @@ -262,15 +262,15 @@ describe Quantity::Unit do
end

it "won't convert a simple unit to another dimension" do
lambda { @foot.convert(:second) }.should raise_error TypeError
lambda { @foot.convert(:second) }.should raise_error ArgumentError
end

it "won't convert a complex unit to a dimension it doesn't contain" do
lambda { @mps.convert(:gram) }.should raise_error TypeError
lambda { @mps.convert(:gram) }.should raise_error ArgumentError
end

it "won't convert to a higher-order unit unless it has an exact matching dimension" do
lambda { @liter.convert(:'mm^2') }.should raise_error TypeError
lambda { @liter.convert(:'mm^2') }.should raise_error ArgumentError
end

it "breaks down named complex units" do
Expand Down