-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistory.rb
More file actions
47 lines (39 loc) · 921 Bytes
/
History.rb
File metadata and controls
47 lines (39 loc) · 921 Bytes
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
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
OR
class Class
def attr_accessor_with_history(attr_name)
attr_name = attr_name.to_s
attr_reader attr_name
attr_reader attr_name + "_history"
class_eval %Q{
def #{attr_name}=(val)
@#{attr_name}=val
@#{attr_name}_history ||= [nil]
@#{attr_name}_history << val
end
}
end
end
class Foo
attr_accessor_with_history :bar
end