This is a multi-part message in MIME format.

------extPart_000_000B_01C313D4.E5A02CA0
Content-Type: text/plain;
	charsetoi8-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. 

P.S. Just in case somebody might be interested:

======== object-space.rb ==============

module ObjectSpace
  def self.find(type,name)
    objects = []
    self.each_object do |_object|
      objects << _object if _object.class == type 
    end
    objects.detect { |_object| _object.to_s == name }
  end
    
  def self.contain?(type,name)
    not find(type,name).nil?
  end

  def self.find_class(name)
    find Class, name
  end 
  
  def self.contain_class?(name)
    contain? Class, name
  end
      
  def self.find_module(name)
    find Module, name
  end
      
  def self.contain_module?(name)
    contain? Module, name
  end
end  

============================================ 
------extPart_000_000B_01C313D4.E5A02CA0
Content-Type: text/html;
	charsetoi8-r"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=koi8-r">
<META content="MSHTML 6.00.2800.1170" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>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:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Courier size=2>require 'object-space'</FONT></DIV>
<DIV><FONT face=Courier size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Courier size=2>if 
ObjectSpace.contain_class?("Test::Unit::TestCase")</FONT></DIV>
<DIV><FONT face=Courier size=2>&nbsp; # do something</FONT></DIV>
<DIV><FONT face=Courier size=2>end</FONT></DIV>
<DIV><FONT face=Courier size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>However, I do not like it as it requires carrying 
aroung an external file with my ObjectSpace extentions.&nbsp;"contain_class?" 
is&nbsp;quite simple,&nbsp;but not simple enough to repeat&nbsp;it in every 
place&nbsp;I may need it.</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Any suggestions?</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>[x86.bison:203]gfb&gt; ruby --version<BR>ruby 1.6.8 
(2002-12-24) [i386-solaris2.8]</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Gennady.</FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>P.S. Just in case&nbsp;somebody might be 
interested:</FONT></DIV>
<DIV><FONT face=Courier size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Courier size=2>======== object-space.rb 
==============</FONT></DIV>
<DIV><FONT face=Courier size=2></FONT>&nbsp;</DIV><FONT face=Arial size=2>
<DIV><FONT face=Courier>module ObjectSpace<BR>&nbsp; def 
self.find(type,name)<BR>&nbsp;&nbsp;&nbsp; objects = []<BR>&nbsp;&nbsp;&nbsp; 
self.each_object do |_object|<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; objects &lt;&lt; 
_object if _object.class == type <BR>&nbsp;&nbsp;&nbsp; 
end<BR>&nbsp;&nbsp;&nbsp; objects.detect { |_object| _object.to_s == name 
}<BR>&nbsp; end<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp; def 
self.contain?(type,name)<BR>&nbsp;&nbsp;&nbsp; not 
find(type,name).nil?<BR>&nbsp; end</FONT></DIV>
<DIV><FONT face=Courier></FONT>&nbsp;</DIV>
<DIV><FONT face=Courier>&nbsp; def self.find_class(name)<BR>&nbsp;&nbsp;&nbsp; 
find Class, name<BR>&nbsp; end <BR>&nbsp; <BR>&nbsp; def 
self.contain_class?(name)<BR>&nbsp;&nbsp;&nbsp; contain? Class, name<BR>&nbsp; 
end<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp; def 
self.find_module(name)<BR>&nbsp;&nbsp;&nbsp; find Module, name<BR>&nbsp; nd<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp; def 
self.contain_module?(name)<BR>&nbsp;&nbsp;&nbsp; contain? Module, name<BR>&nbsp; 
end<BR>end&nbsp;&nbsp;</FONT></DIV>
<DIV><FONT face=Courier></FONT>&nbsp;</DIV>
<DIV><FONT 
face=Courier>============================================</FONT>&nbsp;</FONT></DIV></BODY></HTML>

------extPart_000_000B_01C313D4.E5A02CA0--