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-units

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

Engines and sub-apps

Posted by tom February 25th, 2007

Just came across this article by James W. Long. I think the statement he’s making is a very valuable one: Rails DOES need (and deserve) something better than Engines. Not that engines are bad, on the contrary, it’s a good step in the right direction. And no, Engines are not Evil.