ActiveSupport provides a method Object.subclasses_of, shown below:

def subclasses_of(*superclasses)
    subclasses = []
    ObjectSpace.each_object(Class) do |k|
        next if (k.ancestors & superclasses).empty? ||
superclasses.include?(k) || k.to_s.include?("::") ||
subclasses.include?(k)
        subclasses << k
    end
    subclasses
end

Can anyone shine some light on why it (very deliberately) doesn't
return subclasses defined within modules (ignores classes where
k.to_s.include?("::")).  No tests break if I remove the clause, and
the code appears to have been in the repository since the first
revision, so I can't divine anything looking through changesets. 
Ideally I'd like to use the method myself, but can't unless it works
as I'd expect.  I'd be grateful for any explanation anyone can give.

Tom Ward

P.S. Here's some code to demonstrate what I mean:

class A
end

class B < A
end

Object.subclasses_of(A) # returns [B]

module C
 class D < A
 end
end

Object.subclasses_of(A) # still only returns [B], even though C::D is
also a subclass of A

class E < C::D
end

Object.subclasses_of(A) # returns [B, E], but still not C::D
_______________________________________________
Rails-core mailing list
Rails-core@lists.rubyonrails.org
http://lists.rubyonrails.org/mailman/listinfo/rails-core

Reply via email to