Skip to content

Instantly share code, notes, and snippets.

@Papillard
Last active December 19, 2015 07:29
Show Gist options
  • Save Papillard/5919570 to your computer and use it in GitHub Desktop.
Save Papillard/5919570 to your computer and use it in GitHub Desktop.
Use method_missing hook to define custom methods on Numeric => ex: 3.rupees.in(:euros)
class Numeric
@@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019, 'dollar' => 1.0}
def method_missing( method_id, *args, &block )
singular_ccy = method_id.to_s.gsub( /s$/, '')
if @@currencies.has_key?( singular_ccy )
self * @@currencies[singular_ccy] # Convert into dollars
else
super
end
end
def in( currency )
singular_ccy = currency.to_s.gsub( /s$/, '')
self / @@currencies[singular_ccy] if @@currencies.has_key?( singular_ccy )
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment