This is a multi-part message in MIME format.

------extPart_000_0017_01C313DA.FC44A020
Content-Type: text/plain;
	charsetso-8859-1"
Content-Transfer-Encoding: quoted-printable

Hmm, somehow I overlooked using "defined?" here. After I tried it I got a feeling that I am missing something important. The Pickaxe calls "defined?" "the new predicate method" (p.79). Then if it is a method and not an operator, why Ruby does not complain about uninitialized constant? Like in:

irb(main):001:0> def f
irb(main):002:1> end
=> nil
irb(main):003:0> f Aaa::Bbb::Ccc
NameError: uninitialized constant Aaa
        from (irb):3
irb(main):004:0> defined? Aaa::Bbb::Ccc
=> nil
irb(main):005:0>

In both cases Ruby parses Aaa::Bbb::Ccc before calling a method and is expected to complain.
So, is "defined?" an operator then? I am too lazy today to look into the source.

Gennady.


----- Original Message ----- 
From: "Joel VanderWerf" <vjoel / PATH.Berkeley.EDU>
To: "ruby-talk ML" <ruby-talk / ruby-lang.org>
Sent: Tuesday, May 06, 2003 1:49 PM
Subject: Re: Testing for a class existence


> Gennady wrote:
> > 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
> 
> Why not just
> 
>    if defined?(Test::Unit::TestCase)
> 
> or, if you really want to check that it is a class:
> 
>    if defined?(Test::Unit::TestCase) && Test::Unit::TestCase.is_a?(Class)
> 
> 
> 
------extPart_000_0017_01C313DA.FC44A020
Content-Type: text/html;
	charsetso-8859-1"
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=iso-8859-1">
<META content="MSHTML 6.00.2800.1170" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=Arial size=2>Hmm, somehow I overlooked using "defined?" here. 
After I tried it I got a feeling that I am missing something important. The 
Pickaxe calls "defined?" "the new predicate method" (p.79). Then if it is a 
method and not an operator, why Ruby does not complain about uninitialized 
constant? Like in:</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Courier size=2>irb(main):001:0&gt; def f<BR>irb(main):002:1&gt; 
end<BR>=&gt; nil<BR>irb(main):003:0&gt; f Aaa::Bbb::Ccc<BR>NameError: 
uninitialized constant Aaa<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from 
(irb):3<BR>irb(main):004:0&gt; defined? Aaa::Bbb::Ccc<BR>=&gt; 
nil<BR>irb(main):005:0&gt;</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>In both cases Ruby parses Aaa::Bbb::Ccc before 
calling a method and is expected to complain.</FONT></DIV>
<DIV><FONT face=Arial size=2>So, is "defined?" an operator then? I am too lazy 
today to look into the source.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Gennady.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>----- Original Message ----- </FONT>
<DIV><FONT face=Arial size=2>From: "Joel VanderWerf" &lt;</FONT><A 
href="mailto:vjoel / PATH.Berkeley.EDU"><FONT face=Arial 
size=2>vjoel / PATH.Berkeley.EDU</FONT></A><FONT face=Arial 
size=2>&gt;</FONT></DIV>
<DIV><FONT face=Arial size=2>To: "ruby-talk ML" &lt;</FONT><A 
href="mailto:ruby-talk / ruby-lang.org"><FONT face=Arial 
size=2>ruby-talk / ruby-lang.org</FONT></A><FONT face=Arial 
size=2>&gt;</FONT></DIV>
<DIV><FONT face=Arial size=2>Sent: Tuesday, May 06, 2003 1:49 PM</FONT></DIV>
<DIV><FONT face=Arial size=2>Subject: Re: Testing for a class 
existence</FONT></DIV></DIV>
<DIV><FONT face=Arial><BR><FONT size=2></FONT></FONT></DIV><FONT face=Arial 
size=2>&gt; Gennady wrote:<BR>&gt; &gt; Does anybody know an easy way to test 
for a class/module existence in <BR>&gt; &gt; Ruby? What I do is adding a method 
to ObjectSpace for this, so that I <BR>&gt; &gt; can test it like:<BR>&gt; 
&gt;&nbsp; <BR>&gt; &gt; require 'object-space'<BR>&gt; &gt;&nbsp; <BR>&gt; &gt; 
if ObjectSpace.contain_class?("Test::Unit::TestCase")<BR>&gt; &gt;&nbsp;&nbsp; # 
do something<BR>&gt; &gt; end<BR>&gt; <BR>&gt; Why not just<BR>&gt; <BR>&gt; 
&nbsp;&nbsp; if defined?(Test::Unit::TestCase)<BR>&gt; <BR>&gt; or, if you 
really want to check that it is a class:<BR>&gt; <BR>&gt; &nbsp;&nbsp; if 
defined?(Test::Unit::TestCase) &amp;&amp; 
Test::Unit::TestCase.is_a?(Class)<BR>&gt; <BR>&gt; <BR>&gt; 
</FONT></BODY></HTML>

------extPart_000_0017_01C313DA.FC44A020--