Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Ruby Metaprogramming - SaaS Course - Home Work 01, part 5 a.

By: EDSE on May 29th, 2012  |  syntax: Ruby  |  size: 0.66 KB  |  views: 268  |  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, 'dollar' => 1}
  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. puts "5.euro = "+5.euro.to_s
  20. puts "5.euros = "+5.euros.to_s
  21. puts "5.dollars.in(:euros) = "+5.dollars.in(:euros).to_s
  22. puts "10.euros.in(:rupees) = "+10.euros.in(:rupees).to_s