
Ruby Metaprogramming - SaaS Course - Home Work 01, part 5 a.
By:
EDSE on
May 29th, 2012 | syntax:
Ruby | size: 0.66 KB | hits: 239 | expires: Never
class Numeric
@@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019, 'dollar' => 1}
def method_missing(method, *arg)
singular_currency = method.to_s.gsub(/s$/,'')
if @@currencies.has_key?(singular_currency)
self * @@currencies[singular_currency]
else
super
end
end
def in(arg)
singular_currency = arg.to_s.gsub(/s$/,'')
if @@currencies.has_key?(singular_currency)
self * @@currencies[singular_currency]
end
end
end
puts "5.euro = "+5.euro.to_s
puts "5.euros = "+5.euros.to_s
puts "5.dollars.in(:euros) = "+5.dollars.in(:euros).to_s
puts "10.euros.in(:rupees) = "+10.euros.in(:rupees).to_s