olbrich/ruby-units — pull request #39 by bmishkin (2012-04-17 14:52:37+00:00)
Title: Initialization by hash

Thanks for the great plugin!

I am interested in using Unit(hash) syntax, so I fixed the base library and tests to properly handle several variants of the ```Unit(scalar: i, numerator: n, denominator: d)``` format. I'm not sure if what I added was precisely what was intended or not (permitting non-arrays for numerator, denominator etc) but it's working well for our purposes and passing the tests I added.

The impetus for using hash instead of string initialization, is that we didn't like the implicit conversion of whole-number floats and decimals to fixnums. Example:

```1.9.2p136 :002 > f = 3.000
=> 3.0
1.9.2p136 :003 > f.class
=> Float
1.9.2p136 :005 > Unit("#{f} pounds").scalar.class
=> Fixnum
1.9.2p136 :006 > Unit(scalar: f, numerator: 'pounds').scalar.class
=> Float```

This gets even nastier, once you dig into mathematical operations:
```1.9.2p136 :012 > (Unit("#{f} pounds") / Unit("#{2*f} pounds")).scalar
=> (1/2)
1.9.2p136 :013 > (Unit("#{f} pounds") / Unit("#{2*f} pounds")).scalar.class
=> Rational
1.9.2p136 :014 > (Unit(scalar: f, numerator: 'pounds') / Unit(scalar: 2*f, numerator:'pounds')).scalar
=> 0.5
1.9.2p136 :016 > (Unit(scalar: f, numerator: 'pounds') / Unit(scalar: 2*f, numerator:'pounds')).scalar.class
=> Float```

In our case, we are using floats and want to ensure that resultant Unit objects retain scalar class of Float.