Description
We have been using the RubyUnits gem for quite some time now in our application to perform calculations with units. Sometimes it happens we divide two RubyUnit objects with the same units. In this case the unit information is lost, e.g. Unit.new('10 kg') / Unit.new('5 kg')
equals 2
instead of 2 kg/kg
. I think this is technically correct, but sometimes it would be preferable to keep the unit information to better understand the data you're looking at.
Real world example from our application: we calculate the amount of CO2-emission normalised by something, e.g. the amount of produced product. Both could be measured in weight in kilograms, for example the amount of CO2 per kg produced paper. Now our app shows "CO2 per paper: 2" instead of "CO2 per paper: 2 kg/kg". (We also plot this information in charts and tables over time).
Could you explain why RubyUnits does this and if this is by design? Would you be open to change this behaviour or make it configurable?
Currently RubyUnits also feels a bit inconsistent regarding this behaviour, since it isn't always applied. For example when I initiate a RubyUnits object and pass "kg/kg" myself, this isn't simplified (Unit.new('2 kg/kg') => 2 kg/kg
). Also if we do a division with different units of the same quantity, then the result could be simplified as well, but this doesn't happen (Unit.new('10 kg') / Unit.new('2 g') => 5 kg/g
instead of 5000
). Following the current behaviour of RubyUnits I would expect these cases to be simplified as well.
Although my suggestion would be to not simplify the unit after a division (or multiplication) at all, but only when explicitly asking for it (e.g. by calling to_base
).