This is a multi-part message in MIME format.
------ extPart_000_000B_01C313D4.E5A02CA0
Content-Type: text/plain;
charset oi8-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;
charset oi8-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> </DIV>
<DIV><FONT face=Courier size=2>require 'object-space'</FONT></DIV>
<DIV><FONT face=Courier size=2></FONT> </DIV>
<DIV><FONT face=Courier size=2>if
ObjectSpace.contain_class?("Test::Unit::TestCase")</FONT></DIV>
<DIV><FONT face=Courier size=2> # do something</FONT></DIV>
<DIV><FONT face=Courier size=2>end</FONT></DIV>
<DIV><FONT face=Courier size=2></FONT> </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. "contain_class?"
is quite simple, but not simple enough to repeat it in every
place I may need it.</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>Any suggestions?</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>[x86.bison:203]gfb> ruby --version<BR>ruby 1.6.8
(2002-12-24) [i386-solaris2.8]</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>Gennady.</FONT> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>P.S. Just in case somebody might be
interested:</FONT></DIV>
<DIV><FONT face=Courier size=2></FONT> </DIV>
<DIV><FONT face=Courier size=2>======== object-space.rb
==============</FONT></DIV>
<DIV><FONT face=Courier size=2></FONT> </DIV><FONT face=Arial size=2>
<DIV><FONT face=Courier>module ObjectSpace<BR> def
self.find(type,name)<BR> objects = []<BR>
self.each_object do |_object|<BR> objects <<
_object if _object.class == type <BR>
end<BR> objects.detect { |_object| _object.to_s == name
}<BR> end<BR> <BR> def
self.contain?(type,name)<BR> not
find(type,name).nil?<BR> end</FONT></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV><FONT face=Courier> def self.find_class(name)<BR>
find Class, name<BR> end <BR> <BR> def
self.contain_class?(name)<BR> contain? Class, name<BR>
end<BR> <BR> def
self.find_module(name)<BR> find Module, name<BR> nd<BR> <BR> def
self.contain_module?(name)<BR> contain? Module, name<BR>
end<BR>end </FONT></DIV>
<DIV><FONT face=Courier></FONT> </DIV>
<DIV><FONT
face=Courier>============================================</FONT> </FONT></DIV></BODY></HTML>
------ extPart_000_000B_01C313D4.E5A02CA0--