Doing conversions for you so you don’t have to google them and making code more readable.
Having code that looks like this is meaningless
miles = 8 * 1609.344
You could add comments
miles = 8 * 1609.344 # converting meters to miles
But why not have this!
You can even perform mathematical operations
10.kilometers + 1.mile # 11.609344 kilometers
Handling bytes now works according to the JEDEC memory standard
1.kb.to.b.to_f == 1024.0
To switch to the IEC memory standard, force SI units with
Alchemist::use_si = true
You may also register your own units
Alchemist.register(:distance, [:beard_second, :beard_seconds], 5.angstroms)
Thanks to simonmenke you can now do comparisons without having to convert to floats like so:
5.grams == 0.005.kilograms
gem sources -a http://gemcutter.org sudo gem install alchemist
require 'rubygems' require 'alchemist'
Or if you’re using rails
config.gem 'alchemist', :source => 'http://gemcutter.org'
Alchemist has conversions for:
- metres or meters
- grams or grammes
- troy_pounds or apothecary_pounds
- tons or short_tons
- litres or liters