-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpart6.rb
49 lines (41 loc) · 1.05 KB
/
part6.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Numeric
@@currencies = {'dollar'=> 1,'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019} # dollar => 1 ?
def method_missing(method_id)
singular_currency = method_id.to_s.gsub( /s$/, '')
if @@currencies.has_key?(singular_currency)
self * @@currencies[singular_currency]
else
super
end
end
def in(withans)
singular = withans.to_s.gsub(/s$/,'') # or .chomp('s') altho would that cut an s out of anywhere??
if @@currencies.has_key?(singular)
self * (1/@@currencies[singular])
else
self
end
end
end
class String
# YOUR CODE HERE part b
def palindrome?
p = self.downcase.gsub( /\W/x , "") # might have to change regex to /[^a-z]*\s*\d*/
p == p.reverse
end
end
module Enumerable
# YOUR CODE HERE part c
def palindrome?
palindromecheck = self
palindromecheck == palindromecheck.reverse
end
end
### Test the currency converter
puts 5.dollars.in(:euros)
puts 10.euros.in(:rupees)
puts 10.euro.in(:rupees)
### Test Part B Palindrome
puts "foo".palindrome?
### Test Part C Palindrome
puts [1,2,3,2,1].palindrome?