Guest User

Untitled

a guest
Jan 18th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.66 KB | None | 0 0
  1. # metaprogramming to the rescue!
  2.  
  3. class Numeric
  4.   @@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019, 'auksinas' => 10.09 }
  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.     singular_currency_s = method_id.to_s.gsub( /es$/, '')
  8.     if @@currencies.has_key?(method_id)
  9.       self * @@currencies[method_id]
  10.     elsif @@currencies.has_key?(singular_currency)
  11.       self * @@currencies[singular_currency]
  12.     elsif @@currencies.has_key?(singular_currency_s)
  13.       self * @@currencies[singular_currency_s]
  14.     else
  15.       super
  16.     end
  17.   end
  18. end
Add Comment
Please, Sign In to add comment