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 currency

Here’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}" 

Sorry, comments are closed for this article.