I need to find classes that either:
1) Includes a specific module
or
2) Includes a specfic string in the class name
Either of the two is fine. This is in a Rails environment where I want to
find all classes using one of the two rules above. The problem is that the
classes I want to find probably aren't "required" at the time of searching
(ie ObjectSpace#each_object doesn't find them as far as I can tell and
Module#included hasn't been triggered).
Is it possible to do this?
Best regards,
Marcus
Tried this (doesn't work):
class_names = Array.new
ObjectSpace.each_object { |obj| class_names << obj.name if obj.is_a?(Class)
&& obj.name.include?("ChannelBehaviour") }
behaviours = classes.uniq.map { |name| Class.const_get(name) }
Marcus Bristav wrote:
I need to find classes that either:
1) Includes a specific module
or
2) Includes a specfic string in the class name
require 'enumerator'
ObjectSpace.enum_for(:each_object, Class).select { |x| x.ancestors.include?(Module) }
ObjectSpace.enum_for(:each_object, Class).select { |x| x.name =~ /String/ }
···
--
Florian Frank
Thanks for the answer. Unfortunately this won't work as I explained in my
previous mail. The classes aren't required yet, only on the load path (and
are auto-required by Rails when needed as I understand it), so I need to
find them there somehow.
/Marcus
···
On 8/30/06, Florian Frank <flori@nixe.ping.de> wrote:
Marcus Bristav wrote:
require 'enumerator'
ObjectSpace.enum_for(:each_object, Class).select { |x|
x.ancestors.include?(Module) }
ObjectSpace.enum_for(:each_object, Class).select { |x| x.name =~ /String/
}