Last active
August 29, 2015 13:57
-
-
Save carlossanchezp/9465900 to your computer and use it in GitHub Desktop.
Solución con Method Missing
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
1) Solución con método de clase | |
class Numeric | |
CURRENCIES = {dollar: 1, yen: 0.013, euro: 1.292, rupee: 0.019} "Para versiones superiores a la 1.9 de Ruby" | |
CURRENCIES = {:dollar => 1, :yen => 0.013, :euro => 1.292, :rupee => 0.019} | |
def euros | |
self * CURRENCIES[:euro] | |
end | |
def in(currency) | |
self / CURRENCIES[currency.to_sym] | |
end | |
end | |
2) Solución con method missing | |
============================== | |
class Numeric | |
CURRENCIES = {dollar: 1, yen: 0.013, euro: 1.292, rupee: 0.019} | |
def method_missing(method_id) | |
singular_currency = method_id.to_s | |
if CURRENCIES.has_key?(singular_currency.to_sym) | |
self * CURRENCIES[singular_currency.to_sym] | |
else | |
super | |
end | |
end | |
def in(currency) | |
singular_currency = currency.to_s.gsub(/s$/, '') | |
self / CURRENCIES[singular_currency.to_sym] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment