-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart1.rb
executable file
·58 lines (50 loc) · 1.08 KB
/
part1.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
49
50
51
52
53
54
55
56
57
58
# a)
# metaprogramming to the rescue!
class Numeric
@@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019, 'dollar' => 1.000}
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(currency)
singular_currency = currency.to_s.gsub(/s$/,'')
if @@currencies.has_key?(singular_currency)
self / @@currencies[singular_currency]
end
end
end
=begin
puts 5.dollar.in(:euros)
puts 10.euros.in(:rupees)
=end
# b)
class String
def palindrome?
string = self.downcase.gsub(/\W/, '');
return string == string.reverse;
end
end
=begin
puts "foo".palindrome?
puts "A man, a plan, a canal -- Panama".palindrome?
=end
# c)
module Enumerable
def palindrome?
if self.kind_of? Array
return self == self.reverse
else
return self.to_a.palindrome?
end
end
end
=begin
puts [1,2,3,2,1].palindrome?
puts [1,2,3,2,1,3].palindrome?
ha = {"a" => "b", "c" => "d"}
puts ha.palindrome?
=end