Advertisement
saasbook

metaprogramming to the rescue!

Jan 22nd, 2012
22,987
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.40 KB | None | 0 0
  1. # metaprogramming to the rescue!
  2.  
  3. class Numeric
  4.   @@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019}
  5.   def method_missing(method_id, *args, &block)  # capture all args in case have to call super
  6.     singular_currency = method_id.to_s.gsub( /s$/, '')
  7.     if @@currencies.has_key?(singular_currency)
  8.       self * @@currencies[singular_currency]
  9.     else
  10.       super
  11.     end
  12.   end
  13. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement