From: Gennady Date: 2003-05-07T05:39:21+09:00 Subject: Testing for a class existence This is a multi-part message in MIME format. ------=_NextPart_000_000B_01C313D4.E5A02CA0 Content-Type: text/plain; charset="koi8-r" Content-Transfer-Encoding: quoted-printable Does anybody know an easy way to test for a class/module existence in = Ruby? What I do is adding a method to ObjectSpace for this, so that I = can test it like: require 'object-space' if ObjectSpace.contain_class?("Test::Unit::TestCase") # do something end However, I do not like it as it requires carrying aroung an external = file with my ObjectSpace extentions. "contain_class?" is quite simple, = but not simple enough to repeat it in every place I may need it. Any suggestions? [x86.bison:203]gfb> ruby --version ruby 1.6.8 (2002-12-24) [i386-solaris2.8] Gennady.=20 P.S. Just in case somebody might be interested: =3D=3D=3D=3D=3D=3D=3D=3D object-space.rb = =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D module ObjectSpace def self.find(type,name) objects =3D [] self.each_object do |_object| objects << _object if _object.class =3D=3D type=20 end objects.detect { |_object| _object.to_s =3D=3D name } end =20 def self.contain?(type,name) not find(type,name).nil? end def self.find_class(name) find Class, name end=20 =20 def self.contain_class?(name) contain? Class, name end =20 def self.find_module(name) find Module, name end =20 def self.contain_module?(name) contain? Module, name end end =20 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D ------=_NextPart_000_000B_01C313D4.E5A02CA0 Content-Type: text/html; charset="koi8-r" Content-Transfer-Encoding: quoted-printable
Does anybody know an easy way to test = for a=20 class/module existence in Ruby? What I do is adding a method to = ObjectSpace for=20 this, so that I can test it like:
 
require 'object-space'
 
if=20 ObjectSpace.contain_class?("Test::Unit::TestCase")
  # do something
end
 
However, I do not like it as it = requires carrying=20 aroung an external file with my ObjectSpace = extentions. "contain_class?"=20 is quite simple, but not simple enough to repeat it in = every=20 place I may need it.
 
Any suggestions?
 
[x86.bison:203]gfb> ruby = --version
ruby 1.6.8=20 (2002-12-24) [i386-solaris2.8]
 
Gennady. 
 
P.S. Just in case somebody might = be=20 interested:
 
=3D=3D=3D=3D=3D=3D=3D=3D = object-space.rb=20 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
 
module ObjectSpace
  def=20 self.find(type,name)
    objects =3D = []
   =20 self.each_object do |_object|
      objects = <<=20 _object if _object.class =3D=3D type
   =20 end
    objects.detect { |_object| _object.to_s =3D=3D = name=20 }
  end
   
  def=20 self.contain?(type,name)
    not=20 find(type,name).nil?
  end
 
  def = self.find_class(name)
   =20 find Class, name
  end
 
  def=20 self.contain_class?(name)
    contain? Class, = name
 =20 end
     
  def=20 self.find_module(name)
    find Module, name
  = end
     
  def=20 self.contain_module?(name)
    contain? Module, = name
 =20 end
end  
 
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D<= /FONT> 
------=_NextPart_000_000B_01C313D4.E5A02CA0--