Skip to content

Add support for <unit> per N <unit> #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions lib/ruby_units/unit.rb
Original file line number Diff line number Diff line change
@@ -1511,7 +1511,8 @@ def parse(passed_unit_string = '0')
return
end

if defined?(Rational) && unit_string =~ RATIONAL_NUMBER
power_match = %r{\^\d+/\d+} # This prevents thinking e.g. kg^2/100l is rational because of 2/100
if defined?(Rational) && unit_string =~ RATIONAL_NUMBER && !unit_string.match(power_match)
sign, proper, numerator, denominator, unit_s = unit_string.scan(RATIONAL_REGEX)[0]
sign = sign == '-' ? -1 : 1
rational = sign * (proper.to_i + Rational(numerator.to_i, denominator.to_i))
@@ -1594,7 +1595,7 @@ def parse(passed_unit_string = '0')
if bottom
bottom.gsub!(BOTTOM_REGEX) { "#{Regexp.last_match(1)} " * Regexp.last_match(2).to_i }
# Separate leading decimal from denominator, if any
bottom_scalar, bottom = bottom.scan(NUMBER_UNIT_REGEX)[0]
bottom_scalar, bottom = bottom.scan(NUMBER_UNIT_REGEX)[0] unless self.class.defined?(bottom) # Maintain scalar if it's part of the unit definition
end

@scalar = @scalar.to_f unless @scalar.nil? || @scalar.empty?
88 changes: 88 additions & 0 deletions spec/ruby_units/unit_spec.rb
Original file line number Diff line number Diff line change
@@ -861,6 +861,94 @@
end
end

describe 'Units with rates of <unit> per N <unit>' do
subject { Unit.new(2, 'kg/100kg') }

before do
Unit.clear_cache # Needed for the following definition to consistently work
Unit.define('100kg') do |u|
u.definition = Unit.new('100 kg')
u.aliases = %w[100kg]
end
end

it { is_expected.to be_an_instance_of Unit }

describe '#scalar' do
subject { super().scalar }
it { is_expected.to be_an Numeric }
it { is_expected.to eq(2) }
end

describe '#units' do
subject { super().units }
it { is_expected.to be_a String }
it { is_expected.to eq('kg/100kg') }
end

context 'when there is a power involved (e.g. liters squared)' do
subject { Unit.new(2, 'l^2/100kg') }

it { is_expected.to be_an_instance_of Unit }

describe '#scalar' do
subject { super().scalar }
it { is_expected.to be_an Numeric }
it { is_expected.to eq(2) }
end

describe '#units' do
subject { super().units }
it { is_expected.to be_a String }
it { is_expected.to eq('l^2/100kg') }
end
end

describe 'Calculations' do
describe 'Addition' do
subject { Unit.new(2, 'kg/100kg') + Unit.new(2, 'kg/100kg') }

describe '#scalar', skip: 'TODO: Fix the 4/1 here' do
subject { super().scalar }
it { is_expected.to eq('4') }
end

describe '#units' do
subject { super().units }
it { is_expected.to eq('kg/100kg') }
end
end

describe 'Multiplication' do
subject { Unit.new(2, 'kg/100kg') * Unit.new(2, 'kg/100kg') }

describe '#scalar', skip: 'TODO: Fix the infinite loop here' do
subject { super().scalar }
it { is_expected.to eq('4') }
end

describe '#units', skip: 'TODO: Fix the infinite loop here' do
subject { super().units }
it { is_expected.to eq('kg/100kg') }
end
end

describe 'Division' do
subject { Unit.new(4, 'kg/100kg') / Unit.new(2, 'kg/100kg') }

describe '#scalar' do
subject { super().scalar }
it { is_expected.to eq(2) }
end

describe '#units' do
subject { super().units }
it { is_expected.to eq('') }
end
end
end
end

# time string
describe RubyUnits::Unit.new('1:23:45,200') do
it { is_expected.to be_an_instance_of Unit }