Skip to content

Instantly share code, notes, and snippets.

@EragonJ
Forked from bikewheat/exchangeRate.rb
Created June 19, 2012 02:27
Show Gist options
  • Save EragonJ/2951977 to your computer and use it in GitHub Desktop.
Save EragonJ/2951977 to your computer and use it in GitHub Desktop.
A quick money exchange-rate converter according the newest rate. (Only for Taiwan.) 快速匯率轉換。根據最新台灣銀行匯率。
#encoding: utf-8
require 'net/http'
# Exchange rate word
ex_rate_word = "
新台幣(TWD)
人民幣(CNY)
日圓(JPY)
韓圜(KRW)
港幣(HKD)
泰銖(THB)
新加坡幣(SGD)
印尼盾(IDR)
越南幣(VND)
馬來西亞幣(MYR)
菲律賓披索(PHP)
印度幣(INR)
阿拉伯幣(AED)
科威特幣(KWD)
澳幣(AUD)
紐西蘭幣(NZD)
美元(USD)
加拿大幣(CAD)
巴西幣(BRL)
墨西哥披索(MXN)
阿根廷披索(ARS)
智利披索(CLP)
歐元(EUR)
英鎊(GBP)
俄羅斯盧比(RUB)
瑞士法郎(CHF)
南非幣(ZAR)
瑞典克朗(SEK)"
# Default Value
amt = 1.0
ex_rate1 = "usd".upcase
ex_rate2 = "twd".upcase
# Input from ARGV
if ARGV.length == 3
amt = ARGV[0].to_f
ex_rate1 = ARGV[1].upcase
ex_rate2 = ARGV[2].upcase
end
src = Net::HTTP.get("tw.money.yahoo.com", "/currency_exc_result?amt=#{amt}&from=#{ex_rate1}&to=#{ex_rate2}")
src = src.to_s.force_encoding("UTF-8")
rexp = " = <em>(?<dollars>\.*)<\/em>"
ans = (/#{rexp}/.match(src)[:dollars])
puts amt.to_s + " #{ex_rate1} " + " == " + ans + " #{ex_rate2} "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment