File tree Expand file tree Collapse file tree 2 files changed +22
-5
lines changed Expand file tree Collapse file tree 2 files changed +22
-5
lines changed Original file line number Diff line number Diff line change @@ -1499,12 +1499,22 @@ def coerce(other)
1499
1499
[ self . class . new ( other ) , self ]
1500
1500
end
1501
1501
1502
- # returns a new unit that has been scaled to be more in line with typical usage.
1502
+ # Returns a new unit that has been scaled to be more in line with typical usage. This is highly opinionated and not
1503
+ # based on any standard. It is intended to be used to make the units more human readable.
1504
+ #
1505
+ # Some key points:
1506
+ # * Units containing 'kg' will be returned as is. The prefix in 'kg' makes this an odd case.
1507
+ # * It will use `centi` instead of `milli` when the scalar is between 0.01 and 0.001
1508
+ #
1509
+ # @return [Unit]
1503
1510
def best_prefix
1504
1511
return to_base if scalar . zero?
1512
+ return self if units . include? ( 'kg' )
1505
1513
1506
1514
best_prefix = if kind == :information
1507
1515
self . class . prefix_values . key ( 2 **( ( ::Math . log ( base_scalar , 2 ) / 10.0 ) . floor * 10 ) )
1516
+ elsif ( ( 1 /100 r) ..( 1 /10 r) ) . cover? ( base_scalar )
1517
+ self . class . prefix_values . key ( 1 /100 r)
1508
1518
else
1509
1519
self . class . prefix_values . key ( 10 **( ( ::Math . log10 ( base_scalar ) / 3.0 ) . floor * 3 ) )
1510
1520
end
Original file line number Diff line number Diff line change 2262
2262
specify { expect ( RubyUnits ::Unit . new ( '23 m' ) . div ( RubyUnits ::Unit . new ( '2 m' ) ) ) . to eq ( 11 ) }
2263
2263
end
2264
2264
2265
- context '#best_prefix' do
2266
- specify { expect ( RubyUnits ::Unit . new ( '1024 KiB' ) . best_prefix ) . to eq ( RubyUnits ::Unit . new ( '1 MiB' ) ) }
2267
- specify { expect ( RubyUnits ::Unit . new ( '1000 m' ) . best_prefix ) . to eq ( RubyUnits ::Unit . new ( '1 km' ) ) }
2268
- specify { expect { RubyUnits ::Unit . new ( '0 m' ) . best_prefix } . to_not raise_error }
2265
+ describe '#best_prefix' do
2266
+ it { expect ( RubyUnits ::Unit . new ( '1024 KiB' ) . best_prefix ) . to have_attributes ( scalar : 1 , units : 'MiB' ) }
2267
+ it { expect ( RubyUnits ::Unit . new ( '1000 m' ) . best_prefix ) . to have_attributes ( scalar : 1 , units : 'km' ) }
2268
+ it { expect ( RubyUnits ::Unit . new ( '1/1000 m' ) . best_prefix ) . to have_attributes ( scalar : 1.0 , units : 'mm' ) }
2269
+ it { expect ( RubyUnits ::Unit . new ( '1/100000 m' ) . best_prefix ) . to have_attributes ( scalar : 10 , units : 'um' ) }
2270
+ it { expect ( RubyUnits ::Unit . new ( '0 m' ) . best_prefix ) . to have_attributes ( scalar : 0 , units : 'm' ) }
2271
+ it { expect ( RubyUnits ::Unit . new ( '1000 kg' ) . best_prefix ) . to have_attributes ( scalar : 1000 , units : 'kg' ) }
2272
+ it { expect ( RubyUnits ::Unit . new ( '1_000_000 kg' ) . best_prefix ) . to have_attributes ( scalar : 1_000_000 , units : 'kg' ) }
2273
+ it { expect ( RubyUnits ::Unit . new ( '1/100 m' ) . best_prefix ) . to have_attributes ( scalar : 1 , units : 'cm' ) }
2274
+ it { expect ( RubyUnits ::Unit . new ( '1/10 m' ) . best_prefix ) . to have_attributes ( scalar : 10 , units : 'cm' ) }
2275
+ it { expect ( RubyUnits ::Unit . new ( '0.234 s' ) . best_prefix ) . to have_attributes ( scalar : 234 , units : 'ms' ) }
2269
2276
end
2270
2277
2271
2278
context 'Time helper functions' do
You can’t perform that action at this time.
0 commit comments