Ruby Units
Posted by tom January 6th, 2008
While we’re at it here’s also a little info about the brilliant gem “ruby-units”, which takes away all the nasty conversion from you.
It can be easily installed using the known:
gem install ruby-unitsHere’s some sample code:
require 'rubygems'
gem 'ruby-units'
require 'ruby-units'
c = Unit.new("100cm") >> "ft"
puts c.scalar
k = Unit.new("100","kg") >> "lbs"
puts k.scalar
Ruby Currency
Posted by tom January 6th, 2008
Today, while I was looking how to do Currency conversion in Ruby in a simple manner, using up-to-date rates, I found the currency ruby gem, which can easily be installed using the regular:
gem install currencyHere’s example code (Using RubyGems 1.0.1):
require 'rubygems'
gem 'currency'
require 'currency'
require 'currency/exchange/rate/deriver'
require 'currency/exchange/rate/source/xe'
require 'currency/exchange/rate/source/timed_cache'
# Rate source initialization
provider = Currency::Exchange::Rate::Source::Xe.new
deriver = Currency::Exchange::Rate::Deriver.new(:source => provider)
cache = Currency::Exchange::Rate::Source::TimedCache.new(:source => deriver)
Currency::Exchange::Rate::Source.default = cache
usd = Currency::Money('6.78', :USD)
puts "usd = #{usd.format}"
cad = usd.convert(:CAD)
puts "cad = #{cad.format}"
