Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

Currency Converter (See related posts)

// A simple ruby script to convert currency

#
#   A simple ruby script to convert currency
#     can be edited to check stocks, highs, lows, etc.
#
#    Written by:  Steve at r3lax.com
#
# Usage: money.rb 
# Where:  , -- ISO Currency codes
#
# MUST BE CONNECTED TO THE INTERNET
# Must have ruby-finance installed
require 'finance/currency'

#Take arguments in from command line
c = ARGV

#Ensure that usage is proper
valid = /^([\+-]?\d*(?:\.\d*)?)(\S+)\s(\S+)/
if exchange = valid.match(c)
amount = exchange[1]
from_code = exchange[2]
to_code = exchange[3]

#Let Finance::Currency do its magic
puts Finance::Currency::convert(to_code.to_s, from_code.to_s, amount)
else
#if usage is invalid - inform the user
  puts '-----USAGE IS-----'
  puts ' '
  puts ', -- ISO Currency codes'
end

You need to create an account or log in to post comments to this site.


Related Posts