Initializing with a nil scalar gives unexpected results #40

kranzky opened this Issue Apr 18, 2012 · 2 comments


None yet
2 participants

kranzky commented Apr 18, 2012

$ be irb
rree-1.8.7-2011.03 :001 > require 'ruby-units'
 => true 
ree-1.8.7-2011.03 :002 > nil, "ton/hectare" )
 => 1 ton/hectare 

We do not expect nil to become 1 :)

This bug happens because internally the arguments are converted to " ton/hectare", which parse()somehow extracts a scalar out of.

We'd expect either an ArgumentError to be thrown in this case, or for nil to be represented by a special-case unit.


olbrich commented Apr 18, 2012

This probably happens because Unit("ton/hectare") is equivalent to Unit("1 ton/hectare"). There are times when it makes sense to refer to a unit by itself (e.g, 10 * Unit("ton") => Unit("10 ton")). I can look into tightening this up a bit so it complains if you explicitly set it to nil.

kranzky commented Apr 18, 2012

@olbrich I see; just a bit of background: we're storing ruby-units values in a database by separating then into scalar and units components and storing these in separate columns. we discovered this issue when reading a nil scalar from the database (which can, of course, be acceptable). we can work around this problem in that case, but, in general, having ruby-units complain when being explicitly constructed from a nil scalar would be a good safeguard.

olbrich closed this in 6dee6df Apr 20, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment