Guest User

Untitled

a guest
Jan 17th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 0.98 KB | None | 0 0
  1. class Numeric
  2.   @@currencies = {'dollar' => 1, '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)
  6.       self * @@currencies[singular_currency]
  7.     else
  8.       super
  9.     end
  10. end
  11.  
  12.   def in(cur)
  13.     singular_currency = cur.to_s.gsub(/s$/, '')
  14.     self / @@currencies[singular_currency]
  15.   end
  16.  
  17. end
  18.  
  19.  
  20. def add_method(c, m, &b)
  21.   c.class_eval {
  22.     define_method(m, &b)
  23.   }
  24. end
  25.  
  26. add_method(String, :palindrome?) { self.to_s.gsub(/([-.,\s'!:~?])/, '').reverse.downcase == self.to_s.gsub(/([-.,\s'!~:?])/, '').downcase }
  27.  
  28.  
  29. module Enumerable
  30.   def palindrome?
  31.     if self.is_a?(Hash)
  32.       false        
  33.     else
  34.       self.to_a.join('').gsub(/([-.,\s'!:~?])/, '').reverse.downcase == self.to_a.join('').gsub(/([-.,\s'!~:?])/, '').downcase
  35.     end
  36.   end
  37. end
  38.  
  39.  
  40. puts (1..1).palindrome?
  41. puts "fooof".palindrome?
  42. puts ['1', '2', '1'].palindrome?
Add Comment
Please, Sign In to add comment