Last active
December 19, 2015 07:29
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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