Skip to content

Instantly share code, notes, and snippets.

@carlossanchezp
Last active August 29, 2015 13:57
Show Gist options
  • Save carlossanchezp/9465900 to your computer and use it in GitHub Desktop.
Save carlossanchezp/9465900 to your computer and use it in GitHub Desktop.
Solución con Method Missing
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