Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

ActiveSupport | Module#parent

概要

Module#parent

詳細

Module#parentについて

Module#parent

親モジュールを取得する。
トップレベルまで到達した場合はObjectを返却する。

サンプル

# encoding: utf-8
require 'active_support/core_ext/module/introspection'
require 'tbpgr_utils'

module A
  module B
    module C
      class D
        def hoge
          "hoge"
        end
      end
    end
  end
end

class NoModule
end

bulk_puts_eval binding, <<-EOS
A::B::C::D.parent
A::B::C.parent
A::B.parent
A.parent
NoModule.parent
EOS

__END__
下記はTbpgrUtils gemの機能

bulk_puts_eval

詳しくは下記参照
https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

A::B::C::D.parent # => A::B::C
A::B::C.parent # => A::B
A::B.parent # => A
A.parent # => Object
NoModule.parent # => Object