Shopify / measured

Encapsulate measurements and their units in Ruby and Ruby on Rails.
MIT License
337 stars 28 forks source link

Make adding units with SI prefixes simpler #88

Closed thegedge closed 7 years ago

thegedge commented 7 years ago

I'm thinking we could add an SI helper when building a unit system, which will generate units with the SI prefixes added. Something along the lines:

si_unit :g, aliases: [:gram, :grams]
unit :g, aliases: [:gram, :grams], with_si_prefixes: true

This would generate kilograms, megagrams, micrograms, etc.

kirinrastogi commented 7 years ago

What range would this go to by default? 109 to 10-9 or even more?

kirinrastogi commented 7 years ago

I have started on this, and I don't know if the approach I am taking is good.

I was going to create a class Measured::SIUnitSystemBuilder with def si_unit(unit_name, aliases: [], value: nil, range = 9) The class would have two arrays, each with every 3 prefixes, and they can be as long as needed [:k, :M, :G], and [:m, :μ, :n] would be snippets of them

Then something would iterate over these and call build_unit them, with default aliases "#{unit_name}s" And set the value of the unit relative to the si base unit.

Am I on the right track here? Also, I wasn't sure why with_si_prefixes: true is needed. Can't si_unit build the base unit, as well as ones with all of the prefixes?

thegedge commented 7 years ago

From browsing around the web it looks like there are 20 prefixes (see Wikipedia, for example), so I'd go with those.

I'll address your other questions in your (wip) PR!