Tom Ward
2006-Jan-19 10:28 UTC
Why does Object.subclasses_of ignore subclasses within modules?
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
Obie Fernandez
2006-Jan-19 18:57 UTC
Re: Why does Object.subclasses_of ignore subclasses within modules?
+1 I ran into this myself while doing some complex STI stuff a few months ago Obie On 1/19/06, Tom Ward <tom@popdog.net> wrote:> 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 > > >
Florian Weber
2006-Jan-19 22:37 UTC
Re: Why does Object.subclasses_of ignore subclasses within modules?
I wrote a patch for that a while ago, but then got distracted. Will follow up on it and let you guys know..
James Adam
2006-Jan-19 23:27 UTC
Re: Why does Object.subclasses_of ignore subclasses within modules?
How likely d''you thing it is that this plays a significant part in the oft-discussed ''Rails has problems with modules'' issue? - james On 1/19/06, Florian Weber <csshsh@gmail.com> wrote:> I wrote a patch for that a while ago, but then got distracted. Will > follow up on it and let you guys know.. > _______________________________________________ > Rails-core mailing list > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core >