diff --git a/lib/ruby_units/unit.rb b/lib/ruby_units/unit.rb index 61cfb482..aa588603 100644 --- a/lib/ruby_units/unit.rb +++ b/lib/ruby_units/unit.rb @@ -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 @@ -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 @@ -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) diff --git a/spec/ruby-units/unit_spec.rb b/spec/ruby-units/unit_spec.rb index 11001598..9b4b7356 100644 --- a/spec/ruby-units/unit_spec.rb +++ b/spec/ruby-units/unit_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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"} } @@ -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" } @@ -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" } @@ -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 } @@ -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 @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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 } @@ -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