Skip to content

Instantly share code, notes, and snippets.

@adzap
Created July 29, 2009 12:29
Show Gist options
  • Save adzap/158109 to your computer and use it in GitHub Desktop.
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
# 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