Module: Ruby::Overload::ClassMethods

Defined in:
lib/ruby-overload/overload.rb

Instance Method Summary collapse

Instance Method Details

#method_added(method_name) ⇒ Object



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
# File 'lib/ruby-overload/overload.rb', line 10

def method_added(method_name)
  if @_recurse_catch
    @_recurse_catch = nil
    return
  end

  original_method = instance_method(method_name)

  @_matches ||= Hash.new { |h, k| h[k] = {} }
  @_matches[method_name][original_method.arity] = original_method
  undef_method method_name

  # Prevent recursive calls to method_added if we're doing it
  # intentionally here.
  @_recurse_catch = true

  # Localize for closure
  method_matches = @_matches[method_name]

  if @_methods && @_methods[method_name]
    define_method(method_name, @_methods[method_name])
  else
    define_method(method_name) do |*as|
      method_matches[as.size].bind(self).call(*as)
    end

    @_methods ||= {}
    @_methods[method_name] = instance_method(method_name)
  end
end