Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/ruby_units/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,12 @@ def unitless?
return(@numerator == UNITY_ARRAY && @denominator == UNITY_ARRAY)
end

# returns true if this is a counting unit
# @return [Boolean]
def counting?
self.kind == :unitless && !self.unitless?
end

# Compare two Unit objects. Throws an exception if they are not of compatible types.
# Comparisons are done based on the value of the unit in base SI units.
# @param [Object] other
Expand Down Expand Up @@ -633,6 +639,8 @@ def ==(other)
case
when other.respond_to?(:zero?) && other.zero?
return self.zero?
when self.counting? && other.instance_of?(Unit) && other.counting?
return self.units == other.units
when other.instance_of?(Unit)
return false unless self =~ other
return self.base_scalar == other.base_scalar
Expand All @@ -658,7 +666,7 @@ def ==(other)
def =~(other)
case other
when Unit
self.signature == other.signature
self.signature == other.signature && self.counting? == other.counting?
else
begin
x, y = coerce(other)
Expand Down
39 changes: 39 additions & 0 deletions spec/ruby-units/unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
it { should_not be_degree }
it { should be_base }
it { should be_unitless }
it { should_not be_counting }
it { should be_zero }
its(:base) { should == subject }
end
Expand All @@ -40,6 +41,7 @@
it { should_not be_degree }
it { should be_base }
it { should be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should == subject }
end
Expand All @@ -56,6 +58,7 @@
it { should_not be_degree }
it { should be_base }
it { should be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should == subject }
end
Expand All @@ -72,6 +75,7 @@
it { should_not be_degree }
it { should be_base }
it { should be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should == subject }
end
Expand All @@ -88,6 +92,7 @@
it { should_not be_degree }
it { should be_base }
it { should be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should == subject }
end
Expand All @@ -104,6 +109,7 @@
it { should_not be_degree }
it { should be_base }
it { should be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should == subject }
end
Expand All @@ -119,6 +125,7 @@
it { should_not be_degree }
it { should be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should == subject }
end
Expand All @@ -135,6 +142,7 @@
it { should_not be_degree }
it { should_not be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should == Unit("0.001 m") }
end
Expand All @@ -151,6 +159,7 @@
it { should_not be_degree }
it { should be_base }
it { should be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should == Unit("1") }
end
Expand All @@ -167,6 +176,7 @@
it { should_not be_degree }
it { should_not be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should == Unit("0.001 m") }
end
Expand All @@ -183,6 +193,7 @@
it { should_not be_degree }
it { should_not be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should == Unit("1 kg*m^2/s^2") }
end
Expand All @@ -198,6 +209,7 @@
it { should_not be_degree }
it { should be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should == Unit("10 m/s^2") }
end
Expand All @@ -212,6 +224,7 @@
it { should_not be_degree }
it { should_not be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should be_within(Unit("0.01 m")).of Unit("1.6764 m") }
specify { subject.to_s(:ft).should == %{5'6"} }
Expand All @@ -227,6 +240,7 @@
it { should_not be_degree }
it { should_not be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should be_within(Unit("0.01 kg")).of Unit("2.8633 kg") }
specify { subject.to_s(:lbs).should == "6 lbs, 5 oz" }
Expand All @@ -242,6 +256,7 @@
it { should be_degree }
it { should_not be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should be_within(Unit("0.01 degK")).of Unit("373.15 tempK") }
its(:temperature_scale) { should == "degC" }
Expand All @@ -257,6 +272,7 @@
it { should_not be_degree }
it { should be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should be_a(Numeric) }
its(:temperature_scale) { should be_nil }
Expand All @@ -272,6 +288,7 @@
it { should be_degree }
it { should_not be_base }
it { should_not be_unitless }
it { should_not be_counting }
its(:base) { should be_within(Unit("0.01 degK")).of Unit("100 degK") }
end

Expand All @@ -285,6 +302,7 @@
it { should_not be_degree }
it { should_not be_base }
it { should_not be_unitless }
it { should be_counting }
it { should_not be_zero }
its(:base) { should be_a(Numeric) }
its(:temperature_scale) { should be_nil }
Expand All @@ -300,6 +318,7 @@
it { should_not be_degree }
it { should_not be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should be_a(Numeric) }
its(:temperature_scale) { should be_nil }
Expand All @@ -315,6 +334,7 @@
it { should_not be_degree }
it { should be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should be_a Numeric }
its(:temperature_scale) { should be_nil }
Expand All @@ -330,6 +350,7 @@
it { should_not be_degree }
it { should_not be_base }
it { should_not be_unitless }
it { should be_counting }
it { should_not be_zero }
its(:base) { should be_a Numeric }
its(:temperature_scale) { should be_nil }
Expand All @@ -345,6 +366,7 @@
it { should_not be_degree }
it { should be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should be_a Numeric }
its(:temperature_scale) { should be_nil }
Expand All @@ -360,6 +382,7 @@
it { should_not be_degree }
it { should be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should be_a Numeric }
its(:temperature_scale) { should be_nil }
Expand All @@ -376,6 +399,7 @@
it { should_not be_degree }
it { should_not be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should be_a Numeric }
its(:temperature_scale) { should be_nil }
Expand All @@ -392,6 +416,7 @@
it { should_not be_degree }
it { should_not be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should be_a Numeric }
its(:temperature_scale) { should be_nil }
Expand All @@ -407,6 +432,7 @@
it { should_not be_degree }
it { should_not be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should be_a Numeric }
its(:temperature_scale) { should be_nil }
Expand All @@ -423,6 +449,7 @@
it { should_not be_degree }
it { should_not be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should be_a Numeric }
its(:temperature_scale) { should be_nil }
Expand All @@ -437,6 +464,7 @@
it { should_not be_degree }
it { should be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should be_a Numeric }
its(:temperature_scale) { should be_nil }
Expand All @@ -451,6 +479,7 @@
it { should_not be_degree }
it { should be_base }
it { should_not be_unitless }
it { should_not be_counting }
it { should_not be_zero }
its(:base) { should be_a Numeric }
its(:temperature_scale) { should be_nil }
Expand All @@ -467,6 +496,7 @@
it { should_not be_degree }
it { should_not be_base }
it { should_not be_unitless }
it { should be_counting }
it { should_not be_zero }
its(:base) { should be_a Numeric }
its(:temperature_scale) { should be_nil }
Expand Down Expand Up @@ -1438,3 +1468,12 @@
specify { ((p*v)/(n*r)).convert_to('tempK').should be_within(Unit("0.1 degK")).of(Unit("12027.2 tempK")) }
end
end

describe 'comparing counting units' do
specify { Unit.new('3 dB').should_not eq(Unit.new('3 sr')) }
specify { Unit.new('3 sr').should_not eq(Unit.new('3 nt')) }
specify { Unit.new('3 nt').should_not eq(Unit.new('3 bp')) }
specify { Unit.new('3 bp').should_not eq(Unit.new('3 molecule')) }
specify { Unit.new('3 molecule').should_not eq(Unit.new('3 each')) }
specify { Unit.new('3 each').should_not eq(Unit.new('3')) }
end