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
|
# File 'lib/overload.rb', line 15
def method_added(new_method)
if @evil_things
@evil_things = false
return
end
if kept_methods.nil?
self.ancestors.each_with_index do |ancestor, index|
if ancestor.respond_to?(:kept_methods) && ancestor.kept_methods
self.kept_methods = self.ancestors[index].kept_methods.deep_dup
end
end
end
return unless kept_methods[new_method]
new_method_object = new.method(new_method)
if method_defined?(new_method)
kept_methods[new_method][new_method_object.arity] = new_method_object
instance_variable_set(:@kept_methods, kept_methods)
overload_method = Proc.new do |*args|
kept_methods = self.class.instance_variable_get(:@kept_methods)
if kept_methods[__method__][args.count]
kept_methods[__method__][args.count].call(*args)
else
kept_methods[__method__].keys.sort.each do |arity|
next unless arity < 0 && arity.abs - 1 <= args.count
return kept_methods[__method__][arity].call(*args)
end
end
end
@evil_things = true
define_method new_method, overload_method
end
end
|