$を他の通貨に変換するためのmethod_missingを実装しようとしています。たとえば、5.dollarsは5を生成し、5.yenは0.0655.euro6.56を生成します。これは私が今できることです。今、私はそれを実装する必要がありますが、例えば5.dollars.in(:yen)を実行します。
これは私が今持っているものです:
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)
self * @@currencies[singular_currency]
else
super
end
end
end
誰かが私がこれを行う方法を説明できますか?
PS:コードではなく説明を教えてほしいので、それがどのように行われるかを自分で判断できます。