-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdvancedOOAndMetaProgramming.rb
79 lines (68 loc) · 1.74 KB
/
AdvancedOOAndMetaProgramming.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class Class
attr_reader :boom
def attr_accessor_with_history(attr_name)
attr_name = attr_name.to_s
attr_reader attr_name
attr_reader attr_name+"_history"
define_method "#{attr_name}=" do |val|
myHistory = instance_variable_get "@#{attr_name}_history"
if myHistory == nil
myHistory = []
myHistory.push(nil)
end
myHistory.push(val)
instance_variable_set "@#{attr_name}_history", myHistory
instance_variable_set "@#{attr_name}", val
end
end
end
class Foo
attr_accessor_with_history :bar
end
class Numeric
@@currencies = {'yen' => 0.013, 'euro' => 1.292, 'rupee' => 0.019, 'dollar' => 1}
def in(currency)
singular_currency = currency.to_s.gsub( /s$/, '')
myTimesBy = 1
myDividedBy = 1
mylastOne = @@lastOne.to_s.gsub( /s$/, '')
if mylastOne != "dollar" && singular_currency == "dollar"
if @@currencies.has_key?(singular_currency)
myTimesBy = @@currencies[mylastOne]
myDividedBy = 1
end
elsif mylastOne != "dollar" && singular_currency != "dollar"
myDividedBy = @@currencies[singular_currency]
myTimesBy = @@currencies[mylastOne]
end
self * myTimesBy / myDividedBy
end
def method_missing(method_id)
@@lastOne = "#{method_id}"
singular_currency = method_id.to_s.gsub( /s$/, '')
if @@currencies.has_key?(singular_currency)
self
else
super
end
end
end
class String
def palindrome?()
self.gsub(/\W/, '').downcase == self.gsub(/\W/, '').downcase.reverse
end
end
module Enumerable
def palindrome?
puts "in here!"
self.entries.to_s.gsub(/\W/, '').downcase == self.entries.to_s.gsub(/\W/, '').downcase.reverse
end
end
class Foo
include Enumerable
def each
yield 1
yield 2
yield 1
end
end