Skip to content

Instantly share code, notes, and snippets.

@fearoffish
Created September 19, 2012 12:12
Show Gist options
  • Save fearoffish/3749328 to your computer and use it in GitHub Desktop.
Save fearoffish/3749328 to your computer and use it in GitHub Desktop.
Ruby Units: reliable??
>> Unit('1 g') >> 'ounce'
=> 160000045359237 oz
>> Unit('1 ounce') >> 'g'
=> 453592371600000 g
>> Unit('1 mm').convert_to 'cm'
=> 110 cm
>> Unit('10 ounce') >> 'g'
=> 45359237160000 g
>> Unit('100 ounce') >> 'g'
=> 4535923716000 g
>> Unit('100000 ounce') >> 'g'
=> 4535923716 g
@wjessop
Copy link

wjessop commented Sep 19, 2012

I get slightly different results:

.9.3p194 :002 > require 'ruby-units'
=> true
1.9.3p194 :003 > Unit('1 g') >> 'ounce'
=> 1600000/45359237 oz
1.9.3p194 :004 > Unit('1 ounce') >> 'g'
=> 45359237/1600000 g
1.9.3p194 :005 > Unit('1 mm').convert_to 'cm'
=> 1/10 cm

@fearoffish
Copy link
Author

Interesting, it's Rational numbers. (Thanks to Mon_Ouie on #ruby)

>> Rational(3,4)
=> 34
>> Rational(3,4).inspect
=> "(3/4)"
>> (Unit('100000 ounce') >> 'g').inspect
=> "45359237/16 g"
>> (Unit('1 ounce') >> 'g').inspect
=> "45359237/1600000 g"

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