Question about Module#parent

Module#parent is defined in introspection.rb as

  def parent     parent_name = name.split('::')[0..-2] * '::'     parent_name.empty? ? Object : parent_name.constantize   end

And dependencies.rb defines Class#const_missing as

  def const_missing(class_id)     if [Object, Kernel].include?(self) || parent == self       super     end     ...   end

If the || is not redundant there has to be a self different from Object whose parent is himself, but I can't figure out an example. Just to understand that line, is there any?

-- fxn

For Object, “parent” would be == self, but Object is special-cased in the first half of the condition. IMO, the second condition is redundant.

We need to be certain as well that if parent == self then self == Object.

After thinking about this a bit I think that follows clearly from the definition of Module#parent because the name of a class is unrelated to the constant you use to access to it. So even if you do something perverse as

  class A     A = A   end

where A::A == A holds true (1), it is *not* the case that A::A.parent == A (which would be == self by (1)). On the contrary, it is the case that A::A.parent == A.parent == Object because Module#parent works on the module name.

In addition, as far as I can tell the Kernel in

  [Object, Kernel].include?(self)

is also redundant because that's Class#const_missing. If self is the Kernel module we'll go directly to Module#const_missing.

So the test would be reduced to

  if self == Object # this is a top-level constant     super   else     ...   end

I've uploaded this to Trac:

  http://dev.rubyonrails.org/ticket/10141

-- fxn

We need to be certain as well that if parent == self then self == Object.

This seems like a reasonable assumption, but I'd prefer to hold off changing dependencies.rb until we've released 2.0. Hopefully we'll have a branch for it in a few weeks.

Out of interest, what caused you to start looking around in there?

> We need to be certain as well that if parent == self then self == > Object.

This seems like a reasonable assumption, but I'd prefer to hold off changing dependencies.rb until we've released 2.0. Hopefully we'll have a branch for it in a few weeks.

Sure that's sensible, excellent!

Out of interest, what caused you to start looking around in there?

I am going to give a talk about Rails internals in the next Conferencia Rails Hispana[*]. It covers booting, class loading/ reloading, and request flow. The talk is mainly a code walkthrough so I try to understand each bit of code that goes to the slides (at least as much as I can :-).

In addition, I like reading Rails source code. You know, you understand better the tool you use, you learn more Ruby as a side- effect, ....

Best regards,

-- fxn

[*] http://www.conferenciarails.org/