4 # Returns the name of the module containing this one.
6 # p M::N.parent_name # => "M"
8 unless defined? @parent_name
9 @parent_name = name
=~
/::[^:]+\Z/ ? $
`.freeze : nil
14 # Returns the module which contains this one according to its name.
22 # p M::N.parent # => M
25 # The parent of top-level and anonymous modules is Object.
27 # p M.parent # => Object
28 # p Module.new.parent # => Object
31 parent_name ? parent_name.constantize : Object
34 # Returns all the parents of this module according to its name, ordered from
35 # nested outwards. The receiver is not contained within the result.
43 # p M.parents # => [Object]
44 # p M::N.parents # => [M, Object]
45 # p X.parents # => [M, Object]
50 parts = parent_name.split('::')
52 parents << (parts * '::').constantize
56 parents << Object unless parents.include? Object
60 if RUBY_VERSION < '1.9'
61 # Returns the constants that have been defined locally by this object and
62 # not in an ancestor. This method is exact if running under Ruby 1.9. In
63 # previous versions it may miss some constants if their definition in some
64 # ancestor is identical to their definition in the receiver.
68 ancestors.each do |anc|
70 anc.constants.each { |const| inherited[const] = anc.const_get(const) }
73 constants.select do |const|
74 !inherited.key?(const) || inherited[const].object_id != const_get(const).object_id
78 def local_constants #:nodoc:
83 # Returns the names of the constants defined locally rather than the
84 # constants themselves. See <tt>local_constants</tt>.
85 def local_constant_names
86 local_constants.map { |c| c.to_s }