Advertisement
EDSE

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

May 29th, 2012
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.66 KB | None | 0 0
  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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement