Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 5th, 2012  |  syntax: None  |  size: 0.57 KB  |  hits: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. class Numeric
  2.   @@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019}
  3.   def method_missing(method_id)
  4.     singular_currency = method_id.to_s.gsub( /s$/, '')
  5.     if @@currencies.has_key?(singular_currency) #has_key checks whether or not that key is present in preceeding hash.
  6.       self * @@currencies[singular_currency]
  7.     else
  8.       super
  9.     end
  10.   end
  11.     def in(symb)
  12.         slice_off=symb.to_s.gsub(/s$/,'')
  13.         self/@@currencies[slice_off]
  14.         end
  15.        
  16. end
  17. p 1000.rupees
  18. p 1000.rupees.in(:euros)
  19. =begin
  20. I want something like 5.dollars.in(:euros) and 10.euros.in(:rupees) to work
  21. =end