Skip to content

Instantly share code, notes, and snippets.

@gautamrege
Created May 24, 2012 13:57
Show Gist options
  • Save gautamrege/2781701 to your computer and use it in GitHub Desktop.
Save gautamrege/2781701 to your computer and use it in GitHub Desktop.
Cherry picking Modules
module A
def foo
puts "A:foo"
end
end
module B
def foo
puts "B:foo"
end
end
module C
def self.bar
puts "C:bar"
end
end
class Base
include A
include B
include C
def bar
C.bar
end
def foo(module_name=A)
module_name.instance_method(:foo).bind(self).call
end
end
puts Base.new.is_a?(A) # => true
puts Base.include?(A) # => true
Base.new.foo(B) # => B:foo
Base.new.foo(A) # => A:foo
Base.new.foo # => A:foo
Base.new.bar # => C:bar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment