about const_missing

X

Xavier Noria

Active Support has this code (edited for brevity):

# dependencies.rb ------------------------------------
class Module #:nodoc:
def const_missing(class_id)
Dependencies.load_missing_constant self, class_id
end
end

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

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

Looks like the condition "[Object, Kernel].include?(self) || parent ==
self" wants to test whether this is a root class, but no matter which
is the intention I wonder:

(1) Since Kernel is a module, is there any chance that self
in Class#const_missing is Kernel?

(2) Can parent == self be true with self != Object?

For (2) I've even tried to contrived things like

class A
A = A
end

that makes A::A == A but has not been able to fool Module#parent. I
think "parent == self" is redundant but since it is there I want to be
sure I don't miss some edge case.

-- fxn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top