Skip to content

Instantly share code, notes, and snippets.

Created March 12, 2012 13:41
Show Gist options
  • Save anonymous/2021981 to your computer and use it in GitHub Desktop.
Save anonymous/2021981 to your computer and use it in GitHub Desktop.
class Numeric
@@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019}
def method_missing(method_id)
singular_currency = method_id.to_s.gsub( /s$/, '')
if @@currencies.has_key?(singular_currency) #has_key checks whether or not that key is present in preceeding hash.
self * @@currencies[singular_currency]
else
super
end
end
def in(symb)
slice_off=symb.to_s.gsub(/s$/,'')
self/@@currencies[slice_off]
end
end
p 1000.rupees
p 1000.rupees.in(:euros)
=begin
I want something like 5.dollars.in(:euros) and 10.euros.in(:rupees) to work
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment