Sha256: e129ecf624ac17e83ba073862d70b1c210900fdf48c44d063f285601329e5a89

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

class Module

  # 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
  #
  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

  # Returns all the namespaces of this module according ordered from
  # nearest and moving outwards. The receiver is not contained within
  # the result.
  #
  #   module ::EncExample
  #     module M
  #       module N
  #       end
  #     end
  #   end
  #
  #   EncExample.enclosures        #=> [Object]
  #   EncExample::M.enclosures     #=> [EncExample, Object]
  #   EncExample::M::N.enclosures  #=> [EncExample::M, EncExample, Object]
  #
  def enclosures
    n = []
    name.split(/::/).inject(self) do |mod, cref|
      c = mod.const_get(cref) ; n.unshift(c) ; c
    end
    n << Object # ?
    n.shift # we really don't need +self+ too.
    n
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
facets-2.9.0.pre.1 lib/tour/facets/module/enclosure.rb