Skip to content

Instantly share code, notes, and snippets.

@lewang
Last active December 16, 2015 16:48
Show Gist options
  • Select an option

  • Save lewang/5465395 to your computer and use it in GitHub Desktop.

Select an option

Save lewang/5465395 to your computer and use it in GitHub Desktop.
diff --git a/lib/ruby_units.rb b/lib/ruby_units.rb
index 4aaf462..cd51437 100644
--- a/lib/ruby_units.rb
+++ b/lib/ruby_units.rb
@@ -141,6 +141,8 @@ class Unit
actual_numerator, actual_denominator = actual
new_numerator += actual_numerator
new_denominator += actual_denominator
+ elsif :unitless == Unit.definition(unit_part).kind
+ # ignore unitless, e.g. "%"
else
new_numerator << unit_part
end
@@ -151,6 +153,8 @@ class Unit
actual_numerator, actual_denominator = actual
new_numerator += actual_denominator
new_denominator += actual_numerator
+ elsif :unitless == Unit.definition(unit_part).kind
+ # ignore unitless, e.g. "%"
else
new_denominator << unit_part
end
diff --git a/spec/lib/ruby_units_spec.rb b/spec/lib/ruby_units_spec.rb
index 5096ba0..cc0f558 100644
--- a/spec/lib/ruby_units_spec.rb
+++ b/spec/lib/ruby_units_spec.rb
@@ -121,6 +121,11 @@ describe Unit do
Unit.new("70000 kg*l^2/100l^2*ha").reduce_units.should == Unit.new("7 kg/ha")
end
+ context 'reduces percentages' do
+ out = (Unit.new("5 %") * Unit.new("100 kg")).reduce_units
+ out.should == Unit.new("5 kg")
+ end
+
context 'handle SI prefixes properly' do
# why is left side converted to float? is it because we are not using Rational?
Unit.new("1 lb*kl/l").reduce_units.units == Unit.new("1000 lb").units
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment