Method: Module#method_clash

Defined in:
lib/core/facets/module/method_clash.rb

#method_clash(other) ⇒ Object

Detect method name clash between modules and/or classes, regardless of method visibility:

module MethodClashExample

  module A
    def c; end
  end

  module B
    private
    def c; end
  end

  A.method_clash(B)  #=> [:c]

end

CREDIT: Thomas Sawyer, Robert Dober

Uncommon:

  • require ‘facets/module/method_clash’



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/core/facets/module/method_clash.rb', line 30

def method_clash(other)
  common_ancestor = (ancestors & other.ancestors).first

  s = []
  s += public_instance_methods(true)
  s += private_instance_methods(true)
  s += protected_instance_methods(true)

  o = []
  o += other.public_instance_methods(true)
  o += other.private_instance_methods(true)
  o += other.protected_instance_methods(true)

  c = s & o

  if common_ancestor
    c -= common_ancestor.public_instance_methods(true)
    c -= common_ancestor.private_instance_methods(true)
    c -= common_ancestor.protected_instance_methods(true)
  end

  return c
end