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
29 changes: 20 additions & 9 deletions lib/ruby_units/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Unit < Numeric
:substance,
:luminosity,
:currency,
:memory,
:information,
:angle
]
@@KINDS = {
Expand Down Expand Up @@ -120,7 +120,7 @@ class Unit < Numeric
63999998 => :illuminance,
64000000 => :luminous_power,
1280000000 => :currency,
25600000000 => :memory,
25600000000 => :information,
511999999980 => :angular_velocity,
512000000000 => :angle
}
Expand Down Expand Up @@ -1048,9 +1048,8 @@ def as_json(*args)

# returns the 'unit' part of the Unit object without the scalar
# @return [String]
def units
def units(with_prefix = true)
return "" if @numerator == UNITY_ARRAY && @denominator == UNITY_ARRAY
return @unit_name unless @unit_name.nil?
output_numerator = []
output_denominator = []
num = @numerator.clone.compact
Expand All @@ -1061,7 +1060,9 @@ def units
else
while defn = RubyUnits::Unit.definition(num.shift) do
if defn && defn.prefix?
output_numerator << defn.display_name + RubyUnits::Unit.definition(num.shift).display_name
if with_prefix
output_numerator << (defn.display_name + RubyUnits::Unit.definition(num.shift).display_name)
end
else
output_numerator << defn.display_name
end
Expand All @@ -1073,7 +1074,9 @@ def units
else
while defn = RubyUnits::Unit.definition(den.shift) do
if defn && defn.prefix?
output_denominator << defn.display_name + RubyUnits::Unit.definition(den.shift).display_name
if with_prefix
output_denominator << (defn.display_name + RubyUnits::Unit.definition(den.shift).display_name)
end
else
output_denominator << defn.display_name
end
Expand All @@ -1087,7 +1090,6 @@ def units
map { |x| [x, output_denominator.count(x)] }.
map { |element, power| ("#{element}".strip + (power > 1 ? "^#{power}" : '')) }
out = "#{on.join('*')}#{od.empty? ? '' : '/' + od.join('*')}".strip
@unit_name = out unless self.kind == :temperature
return out
end

Expand Down Expand Up @@ -1264,6 +1266,16 @@ def coerce(other)
end
end

# returns a new unit that has been
def best_prefix
_best_prefix = if (self.kind == :information)
@@PREFIX_VALUES.key(2**((Math.log(self.base_scalar,2) / 10.0).floor * 10))
else
@@PREFIX_VALUES.key(10**((Math.log10(self.base_scalar) / 3.0).floor * 3))
end
self.to(RubyUnits::Unit.new(@@PREFIX_MAP.key(_best_prefix)+self.units(false)))
end

# Protected and Private Functions that should only be called from this class
protected

Expand Down Expand Up @@ -1300,6 +1312,7 @@ def unit_signature_vector
return vector
end


private

# used by #dup to duplicate a Unit
Expand Down Expand Up @@ -1516,8 +1529,6 @@ def self.base_units
return @@base_units ||= @@definitions.dup.delete_if { |_, defn| !defn.base? }.keys.map { |u| RubyUnits::Unit.new(u) }
end

private

# parse a string consisting of a number and a unit string
# @param [String] string
# @return [Array] consisting of [Numeric, "unit"]
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_units/unit_definitions/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
unit.scalar = 1
unit.numerator = %w{<byte>}
unit.aliases = %w{B byte bytes}
unit.kind = :memory
unit.kind = :information
end

RubyUnits::Unit.define("dollar") do |unit|
Expand Down
5 changes: 5 additions & 0 deletions spec/ruby-units/unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,11 @@
specify { Unit('23 m').div(Unit('2 m')).should == 11 }
end

context '#best_prefix' do
specify { Unit('1024 KiB').best_prefix.should == Unit('1 MiB')}
specify { Unit('1000 m').best_prefix.should == Unit('1 km')}
end

context "Time helper functions" do
before do
Time.stub!(:now).and_return(Time.utc(2011,10,16))
Expand Down