Feature #3773: Module#parent
http://redmine.ruby-lang.org/issues/show/3773
Author: Thomas Sawyer
Status: Open, Priority: Normal
Not sure "parent" is the best name, it does seem like this functionality
should be provided somehow by Ruby itself.
class Module
# Returns the name of the module containing this one.
#
# M::N.parent_name # => "M"
def parent_name
unless defined? @parent_name
@parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
end
@parent_name
end
# Returns the module which contains this one according to its name.
#
# module M
# module N
# end
# end
# X = M::N
#
# M::N.parent # => M
# X.parent # => M
#
# The parent of top-level and anonymous modules is Object.
#
# M.parent # => Object
# Module.new.parent # => Object
#
def parent
parent_name ? constant(parent_name) : Object
end
# Returns all the parents of this module according to its name,
ordered from
# nested outwards. The receiver is not contained within the result.
#
# module M
# module N
# end
# end
# X = M::N
#
# M.parents # => [Object]
# M::N.parents # => [M, Object]
# X.parents # => [M, Object]
#
def parents
parents = []
if parent_name
parts = parent_name.split('::')
until parts.empty?
parents << constant(parts * '::')
parts.pop
end
end
parents << Object unless parents.include? Object
parents
end
end
on 2010-08-31 17:33
on 2010-08-31 18:12
Hi,
In message "Re: [ruby-core:31969] [Ruby 1.9-Feature#3773][Open]
Module#parent"
on Wed, 1 Sep 2010 00:24:25 +0900, Thomas Sawyer
<redmine@ruby-lang.org> writes:
|Not sure "parent" is the best name, it does seem like this functionality should be provided somehow by Ruby itself.
Objection.
(a) I don't think "parent" is a proper name for the feature.
(b) returning Object for anonymous module is weird behavior.
matz.
on 2010-09-03 13:00
Issue #3773 has been updated by Thomas Sawyer.
I agree and I have another objection to add.
(c) A constant name is just a reference to an object.
That is to say, like any variable name, the object does not know it has
been assigned it. If someone did C::D = A::B, then what would
C::D.parent be?
However my objection also argues against having Module#name at all. So
perhaps for modules their is a necessary exception? Actually, this is
partly why I bring up this suggested feature --to better understand why
modules even know their names in the first place.
As for objection (a) I have seen other suggestions: enclosing_module,
enclosure, container, namespace, enclosing_namespace, modspace, etc.
For objection (c), that's a good point. nil would be a better return
value in that case.
trans.
----------------------------------------
http://redmine.ruby-lang.org/issues/show/3773
on 2010-09-03 13:03
Issue #3773 has been updated by Thomas Sawyer. "For objection (c), that's a good point. nil would be a better return value in that case." I meant, "objection (b)". matz, do you have a preference for a name? ---------------------------------------- http://redmine.ruby-lang.org/issues/show/3773
on 2010-09-06 16:39
Hi,
In message "Re: [ruby-core:32038] [Ruby 1.9-Feature#3773] Module#parent"
on Fri, 3 Sep 2010 20:03:34 +0900, Thomas Sawyer
<redmine@ruby-lang.org> writes:
|Issue #3773 has been updated by Thomas Sawyer.
|
|
|"For objection (c), that's a good point. nil would be a better return value in that case."
|
|I meant, "objection (b)".
|
|matz, do you have a preference for a name?
As pointed by objection (c), a constant name is just a reference to
a module/class object. For example, the following code creates two
references to the A::B module:
module A
module B
end
end
C = A::B
Although a module records the first reference name, just for
convenience, having a method to retrieve "a parent of the primary
reference" makes me feel bit weird.
If you show me a real use-case, I might feel different.
matz.
on 2010-09-07 16:22
Issue #3773 has been updated by Thomas Sawyer. The use cases that I am aware of are from Rails: * http://github.com/rails/rails/blob/master/activesupport/lib/active_support/dependencies.rb#L496 * http://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations.rb#L1827 ---------------------------------------- http://redmine.ruby-lang.org/issues/show/3773
on 2010-09-07 17:46
Hi,
In message "Re: [ruby-core:32103] [Ruby 1.9-Feature#3773] Module#parent"
on Tue, 7 Sep 2010 23:19:43 +0900, Thomas Sawyer
<redmine@ruby-lang.org> writes:
|The use cases that I am aware of are from Rails:
|
|* http://github.com/rails/rails/blob/master/activesupport/lib/active_support/dependencies.rb#L496
|* http://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations.rb#L1827
Well, the former implicitly assumes specific structure of loading
libraries, which is not supported by Ruby itself. The latter,
..well.. has proven how Ruby+activesupport looks differently from plain
Ruby ;-)
In any way, when ActiveSupport defines #parent, I don't see any reason
to add it to the core. Besides that #parent can be easily confused
with #superclass.
matz.
on 2010-09-14 11:24
Issue #3773 has been updated by Thomas Sawyer. Mainly I suggest it be added to core on the basis that 1) since we already have Module#name we might as well have this too, and 2) it is very inefficient and non-robust to code in pure Ruby. And yes, I agree that #parent is the wrong name. What would you call it? I want to know b/c, even if you do not add it to core, I want to add it to Ruby Facets, but I am not sure what to call it myself. ---------------------------------------- http://redmine.ruby-lang.org/issues/show/3773
on 2010-09-25 19:03
Issue #3773 has been updated by Thomas Sawyer. Had a thought today about this. How about calling it `home`? That semantically makes sense with the fact that it is actually just a reference and therefore could be assigned to other names and thus other "enclosures", but still it would have only one "home" --the original place of definition. ---------------------------------------- http://redmine.ruby-lang.org/issues/show/3773
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.