UMLETA.COM - software tools & development
software development and tools

Currency: Ruby Package for FX and Money

Submitted by Kurt on October 29, 2006 - 10:03pm.

See: http://rubyforge.org/projects/currency/

The RubyForge package currency
implements an object-oriented representation of currencies, monetary values, foreign exchanges and rates.

Currency::Money uses a scaled integer representation of the monetary value and performs accurate conversions to and from string values.

Currency::Currency
provides an object-oriented representation of a currency.

Currency::Exchange::Base
represents the base class for a currency exchange rate
provider.

require 'currency'
require 'currency/exchange/xe'

usd = Currency::Money('6.78', :USD)
puts "usd = #{usd.format}"
cad = usd.convert(:CAD)
puts "cad = #{cad.format}"

The example above, uses Currency::Exchange::Xe
to automatically get exchange rates from http://xe.com/.

Future enhancements will include:

  • storage and retrival of historical exchange rates
  • exchange rate polling for population of historical rates
  • support for inflationary rates within a currency, e.g. $10 USD in the year 1955 in 2006 USD.

This package also contains ActiveRecord support for money values:

require 'currency'
require 'currency/active_record'

class Entry < ActiveRecord::Base
   money :amount
end


In the example above, the entries.amount database column is an INTEGER that represents US cents. The currency code of the money value can be stored in an additional database column or a default currency can be used.

The first file release is pending, so please visit the svn repo at:

svn checkout svn://rubyforge.org/var/svn/currency/currency/trunk

For more examples, see:

I'd love to hear your feedback on this module.

-- Kurt

links: add new comment

Ruby Gem available


A rubygem for this package is now available on http://rubyforge.org:

gem install currency

-- Kurt