Ruby: получите список расширенных модулей?

Когда вы включаете модули в класс или другой модуль, вы можете вызвать

@mymod.included_modules

чтобы получить список включенных модулей.

Есть ли эквивалент для перечисления модулей, модуль был расширен на?

module Feature1
end

module Feature2
  extend Feature1
end

Feature2.extended_modules #=> [Feature1]

Ответы

Ответ 1

Они там, вам просто нужно посмотреть в нужное место:

(class << Feature2; self end).included_modules   # [Feature1, Kernel]

Мы можем обобщить следующее:

class Module
  # Return any modules we +extend+
  def extended_modules
    (class << self; self end).included_modules
  end
end

# Now get those extended modules peculiar to Feature2
Feature2.extended_modules - Module.extended_modules # [Feature1]

Ответ 2

Feature2.singleton_class.included_modules # => [Feature1, ...]

Ответ 3

Все модули Ruby могут быть перечислены в CLI (Command Line), как показано ниже:

ruby -e 'puts Gem::Specification.all().map{|g| [g.name, g.version.to_s] }'

ИЛИ

ruby -rubygems -e 'puts Gem::Specification.all().map{|g| [g.name, g.version.to_s] }'

Надеюсь, это поможет в некоторой степени!!!