From: Gennady Date: 2003-05-07T06:23:06+09:00 Subject: Re: Testing for a class existence This is a multi-part message in MIME format. ------=_NextPart_000_0017_01C313DA.FC44A020 Content-Type: text/plain; charset="iso-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 =3D> nil irb(main):003:0> f Aaa::Bbb::Ccc NameError: uninitialized constant Aaa from (irb):3 irb(main):004:0> defined? Aaa::Bbb::Ccc =3D> 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 -----=20 From: "Joel VanderWerf" To: "ruby-talk ML" 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=20 > > Ruby? What I do is adding a method to ObjectSpace for this, so that = I=20 > > can test it like: > > =20 > > require 'object-space' > > =20 > > if ObjectSpace.contain_class?("Test::Unit::TestCase") > > # do something > > end >=20 > Why not just >=20 > if defined?(Test::Unit::TestCase) >=20 > or, if you really want to check that it is a class: >=20 > if defined?(Test::Unit::TestCase) && = Test::Unit::TestCase.is_a?(Class) >=20 >=20 > ------=_NextPart_000_0017_01C313DA.FC44A020 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Hmm, somehow I overlooked using = "defined?" here.=20 After I tried it I got a feeling that I am missing something important. = The=20 Pickaxe calls "defined?" "the new predicate method" (p.79). Then if it = is a=20 method and not an operator, why Ruby does not complain about = uninitialized=20 constant? Like in:
 
irb(main):001:0> def = f
irb(main):002:1>=20 end
=3D> nil
irb(main):003:0> f Aaa::Bbb::Ccc
NameError:=20 uninitialized constant Aaa
        = from=20 (irb):3
irb(main):004:0> defined? Aaa::Bbb::Ccc
=3D>=20 nil
irb(main):005:0>
 
In both cases Ruby parses Aaa::Bbb::Ccc = before=20 calling a method and is expected to complain.
So, is "defined?" an operator then? I = am too lazy=20 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=20 existence

> Gennady wrote:
> > Does anybody know an easy way = to test=20 for a class/module existence in
> > Ruby? What I do is adding = a method=20 to ObjectSpace for this, so that I
> > can test it = like:
>=20 > 
> > require 'object-space'
> >  =
> >=20 if ObjectSpace.contain_class?("Test::Unit::TestCase")
> = >   #=20 do something
> > end
>
> Why not just
> =
>=20    if defined?(Test::Unit::TestCase)
>
> or, if = you=20 really want to check that it is a class:
>
>    = if=20 defined?(Test::Unit::TestCase) &&=20 Test::Unit::TestCase.is_a?(Class)
>
>
>=20
------=_NextPart_000_0017_01C313DA.FC44A020--