Created
July 29, 2009 12:29
-
-
Save adzap/158109 to your computer and use it in GitHub Desktop.
Ruby extended_modules method which returns all modules a class or module has been extended with
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Defines a method on modules and classes to output the modules it has been extended with. | |
# This works because extending class really just includes the module into the classes metaclass. | |
class Module | |
def extended_modules | |
# this exposes the metaclass and for any code inside, self is scoped to the metaclass | |
class << self | |
self.included_modules | |
end | |
end | |
end | |
# Example: | |
# >> class A; end | |
# >> module Stuff; end | |
# >> A.extend Stuff | |
# >> A.extended_modules | |
# => [Stuff, Kernel] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment