Skip to content

Instantly share code, notes, and snippets.

@dblack
Last active December 14, 2015 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dblack/5066855 to your computer and use it in GitHub Desktop.
Save dblack/5066855 to your computer and use it in GitHub Desktop.
class A # top-level A
end
module M
module ClassMethods
def create_an_a
A.new # top-level A
end
end
module InstanceMethods
def do_something
end
end
end
class B
class A # nested A
extend M::ClassMethods
include M::InstanceMethods
end
def go
an_a = A.create_an_a # Calling a method on *nested* A that
an_a.do_something # happens to return an instance of
end # *top-level* A.
end
B.new.go # unknown method do_something, because the A object you're
# calling it on is an instance of top-level A,
# not an instance of nested A, and nested A is
# the one with the module included.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment