Method: Module#enclosure

Defined in:
lib/core/facets/module/enclosure.rb

#enclosureObject

Returns the module which contains this one according to its name.

module ::EncExample
  module M
    module N
    end
  end
end

EncExample::M::N.enclosure  #=> EncExample::M

The enclosure of top-level and anonymous modules is Object.

EncExample.enclosure   #=> Object
Module.new.enclosure   #=> Object

NOTE: This method is not a common core extension and is not loaded automatically when using require 'facets'.

Uncommon:

  • require ‘facets/module/enclosure’



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/core/facets/module/enclosure.rb', line 25

def enclosure
  name = /::[^:]+\Z/ =~ self.name ? $` : nil
  if name
    #base = name.sub!(/^::/, '') ? Object : self
    name.split(/::/).inject(self) do |mod, cref|
      if /\:(0x.*?)\>$/ =~ cref   # TODO: does this ever happen?
        #p $1.to_i(16)
        ObjectSpace._idref($1.to_i(16))
      else
        mod.const_get(cref)
      end
    end
  else
    Object
  end
end