Skip to content
Merged
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
4 changes: 2 additions & 2 deletions lib/ruby_units/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ def +(other)
RubyUnits::Unit.new(scalar: (other.scalar + convert_to(other.temperature_scale).scalar), numerator: other.numerator, denominator: other.denominator, signature: other.signature)
end
else
RubyUnits::Unit.new(scalar: (base_scalar + other.base_scalar), numerator: base.numerator, denominator: base.denominator, signature: @signature).to(units)
RubyUnits::Unit.new(scalar: (base_scalar + other.base_scalar), numerator: base.numerator, denominator: base.denominator, signature: @signature).convert_to(self)
end
else
raise ArgumentError, "Incompatible Units ('#{self}' not compatible with '#{other}')"
Expand Down Expand Up @@ -883,7 +883,7 @@ def -(other)
elsif other.temperature?
raise ArgumentError, 'Cannot subtract a temperature from a differential degree unit'
else
RubyUnits::Unit.new(scalar: (base_scalar - other.base_scalar), numerator: base.numerator, denominator: base.denominator, signature: @signature).to(units)
RubyUnits::Unit.new(scalar: (base_scalar - other.base_scalar), numerator: base.numerator, denominator: base.denominator, signature: @signature).convert_to(self)
end
else
raise ArgumentError, "Incompatible Units ('#{self}' not compatible with '#{other}')"
Expand Down
8 changes: 8 additions & 0 deletions spec/ruby_units/unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,10 @@
context 'between a degree and a temperature' do
specify { expect(RubyUnits::Unit.new('100 degK') + RubyUnits::Unit.new('100 tempK')).to eq(RubyUnits::Unit.new('200 tempK')) }
end

context 'between two unitless units' do
specify { expect(RubyUnits::Unit.new('1') + RubyUnits::Unit.new('2')).to eq 3 }
end
end

context 'subtracting (-)' do
Expand Down Expand Up @@ -1784,6 +1788,10 @@
context 'between a degree and a temperature' do
specify { expect { (RubyUnits::Unit.new('100 degK') - RubyUnits::Unit.new('100 tempK')) }.to raise_error(ArgumentError, 'Cannot subtract a temperature from a differential degree unit') }
end

context 'a unitless unit from another unitless unit' do
specify { expect(RubyUnits::Unit.new('1') - RubyUnits::Unit.new('2')).to eq -1 }
end
end

context 'multiplying (*)' do
Expand Down