Looking good.
Some volume conversion seems to be wrong thought:
irb(main):007:0> 1.teaspoons.to_cubic_meters ** (1.0 / 3)
=> 58.7601229917626
That's a very deep Olympic size swimming pool!
Trusting google for unit conversion:
[gus@comp tmp]$ ruby get_standard.rb 2>/dev/null
:pints => 0.125,
:milliliters => 0.000264172051,
:cubic_feet => 7.48051945,
:cups => 0.0625,
:microliters => 2.64172051,
:cubic_inches => 0.00432900431,
:liters => 0.264172051,
:cubic_centimeters => 0.000264172051,
:cubic_yards => 201.974025,
:hectoliters => 26.4172051,
:fluid_ounces => 0.0078125,
:cubic_millimeters => 2.64172051,
:gallons => 1,
:deciliters => 0.0264172051,
:tablespoons => 0.00390625,
:cubic_meters => 264.172051,
:quarts => 0.25,
:centiliters => 0.00264172051,
:teaspoons => 0.00130208333,
:cubic_decimeters => 0.264172051,
[gus@comp tmp]$ cat get_standard.rb
require 'units/standard'
require 'net/http'
h = Net::HTTP.new('www.google.com', 80)
Numeric::VOLUME.keys.each do |u|
v = u.to_s.gsub(/_/, " ")
resp, data = h.get("/search?q=1+#{v.gsub(/ /, "+")}+in+gallon", nil)
if data =~ /\(?#{v}\)?\s*=\s*([\d.]+)/
val = $1
puts(":%-20s => #{val}," % u)
else
$stderr.puts("#{u} failed")
end
end
Thank you for your library,
Guillaume.
···
On Wed, 2005-09-07 at 07:46 +0900, Lucas Carlson wrote:
I have also created a new library to add units to numbers in Ruby:
gem install units
http://units.rubyforge.org/
It is extremely simple to use:
require 'units/standard'
1.lb.to_ounces # => 16.0
require 'units/currency'
1.euro.usd # => 1.2545
1.usd.unit # => :usd
1.usd.to_yen # => 108.9 # this information is grabbed on the fly
via a SOAP call
1.usd.to_yet.unit # => :yen
It is also very simple to add both static and dynamic conversions on
your own via add_unit_conversions and add_unit_aliases.
I hope you find this library useful too.