Skip to content

Instantly share code, notes, and snippets.

@sirmxanot
Created November 19, 2012 17:52
Show Gist options
  • Save sirmxanot/4112293 to your computer and use it in GitHub Desktop.
Save sirmxanot/4112293 to your computer and use it in GitHub Desktop.
currency converter with symbols, singular, plural
class Numeric
@@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019}
@@currencies['dollar']=1
def method_missing(method_id)
singular_currency = method_id.to_s.gsub( /s$/, '')
if @@currencies.has_key?(singular_currency)
self * @@currencies[singular_currency]
else
super
end
end
def in(currency)
if @@currencies.has_key?(currency)
self / @@currencies[currency]
elsif @@currencies.has_key?(currency.to_s.gsub( /^:|s$/, ''))
self / @@currencies[currency.to_s.gsub( /^:|s$/, '')]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment