1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix Object.subclasses_of to only return currently defined objects (closes #3882) [Jonathan Viney <jonathan@bluewire.net.nz>]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3607 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson 2006-02-19 01:05:55 +00:00
parent 8896efd41a
commit 9723d03531
3 changed files with 13 additions and 2 deletions

View file

@ -1,5 +1,7 @@
*SVN* *SVN*
* Fix Object.subclasses_of to only return currently defined objects [Jonathan Viney <jonathan@bluewire.net.nz>]
* Fix constantize to properly handle names beginning with '::'. [Nicholas Seckar] * Fix constantize to properly handle names beginning with '::'. [Nicholas Seckar]
* Make String#last return the string instead of nil when it is shorter than the limit [Scott Barron]. * Make String#last return the string instead of nil when it is shorter than the limit [Scott Barron].

View file

@ -8,7 +8,7 @@ class Object #:nodoc:
def subclasses_of(*superclasses) def subclasses_of(*superclasses)
subclasses = [] subclasses = []
ObjectSpace.each_object(Class) do |k| ObjectSpace.each_object(Class) do |k|
next if (k.ancestors & superclasses).empty? || superclasses.include?(k) || k.to_s.include?("::") || subclasses.include?(k) next if (k.ancestors & superclasses).empty? || superclasses.include?(k) || k.to_s.include?("::") || subclasses.include?(k) || !Object.const_defined?(k.to_s.to_sym)
subclasses << k subclasses << k
end end
subclasses subclasses

View file

@ -7,6 +7,9 @@ class ClassB < ClassA; end
class ClassC < ClassB; end class ClassC < ClassB; end
class ClassD < ClassA; end class ClassD < ClassA; end
class ClassI; end
class ClassJ < ClassI; end
module Bar module Bar
def bar; end def bar; end
end end
@ -31,6 +34,12 @@ class ClassExtTest < Test::Unit::TestCase
assert !defined?(ClassC) assert !defined?(ClassC)
assert !defined?(ClassD) assert !defined?(ClassD)
end end
def test_subclasses_of
assert_equal [ClassJ], Object.subclasses_of(ClassI)
ClassI.remove_subclasses
assert_equal [], Object.subclasses_of(ClassI)
end
end end
class ObjectTests < Test::Unit::TestCase class ObjectTests < Test::Unit::TestCase