Skip to content

Instantly share code, notes, and snippets.

@paneq
Created August 6, 2012 10:15
Show Gist options
  • Save paneq/3273206 to your computer and use it in GitHub Desktop.
Save paneq/3273206 to your computer and use it in GitHub Desktop.
Prevent including module if target already has some of its instance methods
module A
extend SelfishModule
def abc; end
end
module B
extend SelfishModule
def bcd; end
end
module Aa
extend SelfishModule
def abc; end
end
o = Object.new
o.extend(A)
o.extend(B)
o.extend(Aa)
module A
extend SelfishModule
def abc; end
end
module B
extend SelfishModule
def bcd; end
end
module Aa
extend SelfishModule
def abc; end
end
class X
include A
include B
include Aa
end
RuntimeError: Methods already implemented: [:abc]
module SelfishModule
def append_features(target)
target_methods = target.instance_methods + target.private_instance_methods
my_methods = instance_methods + private_instance_methods
common_methods = target_methods & my_methods
raise "Methods already implemented: #{common_methods}" unless common_methods.empty?
super
end
def extend_object(target)
target_methods = target.methods(true) + target.private_methods(true)
my_methods = instance_methods + private_instance_methods
common_methods = target_methods & my_methods
raise "Methods already implemented: #{common_methods}" unless common_methods.empty?
super
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment