peepraeza

Untitled

Sep 8th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.54 KB | None | 0 0
  1. class Numeric
  2.   @@currencies = {'dollar' => 1, 'yen' => 107.85, 'euro' => 0.83, 'baht' => 33.12}
  3.   def method_missing(method, *arg)
  4.     singular_currency = method.to_s.gsub(/s$/,'')
  5.     if @@currencies.has_key?(singular_currency)
  6.       self * @@currencies[singular_currency]
  7.     else
  8.       super
  9.     end
  10.   end
  11.   def in(arg)
  12.     singular_currency = arg.to_s.gsub(/s$/,'')
  13.     if @@currencies.has_key?(singular_currency)
  14.       self * @@currencies[singular_currency]
  15.     end
  16.   end
  17. end
  18.  
  19. val = (1.dollars - 1.euro).in(:dollars)
  20. puts val
Advertisement
Add Comment
Please, Sign In to add comment