Testing for a class existence

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

···

============================================

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)

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)

You can always look in the Pickaxe :slight_smile: Yes, it’s an operator:

Ruby supports all the standard boolean operators and introduces the
new operator defined?.

The defined? operator returns nil if its argument (which can be an
arbitrary expression) is not defined, otherwise it returns a
description of that argument. If the argument is yield, defined?
returns the string `yield’’ if a code block is associated with the
current context.

defined? 1 » “expression”
defined? dummy » nil
defined? printf » “method”
defined? String » “constant”
defined? $& » nil
defined? $_ » “global-variable”
defined? Math::PI » “constant”
defined? ( c,d = 1,2 ) » “assignment”
defined? 42.abs » “method”

http://www.rubycentral.com/book/tut_expressions.html
(although I keep a local copy for easy grepability :slight_smile:

Regards,

Brian.

···

On Wed, May 07, 2003 at 06:23:06AM +0900, Gennady wrote:

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.

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.

You can always look in the Pickaxe :slight_smile: Yes, it’s an operator:

Ruby supports all the standard boolean operators and introduces the
new operator defined?.

The defined? operator returns nil if its argument (which can be an

Interesting. In my Pickaxe hardcopy it says “The defined? method returns nil
if …” and “… the new predicate method defined?.” a little bit earlier.
Must be some typo.

···

----- Original Message -----
From: “Brian Candler” B.Candler@pobox.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Tuesday, May 06, 2003 2:35 PM
Subject: Re: Testing for a class existence

On Wed, May 07, 2003 at 06:23:06AM +0900, Gennady wrote:

arbitrary expression) is not defined, otherwise it returns a
description of that argument. If the argument is yield, defined?
returns the string `yield’’ if a code block is associated with the
current context.

defined? 1 » “expression”
defined? dummy » nil
defined? printf » “method”
defined? String » “constant”
defined? $& » nil
defined? $_ » “global-variable”
defined? Math::PI » “constant”
defined? ( c,d = 1,2 ) » “assignment”
defined? 42.abs » “method”

http://www.rubycentral.com/book/tut_expressions.html
(although I keep a local copy for easy grepability :slight_smile:

Regards,

Brian.

In article 20030506213500.GA49605@uk.tiscali.com,

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.

You can always look in the Pickaxe :slight_smile: Yes, it’s an operator:

Ruby supports all the standard boolean operators and introduces the
new operator defined?.

The defined? operator returns nil if its argument (which can be an
arbitrary expression) is not defined, otherwise it returns a
description of that argument. If the argument is yield, defined?
returns the string `yield’’ if a code block is associated with the
current context.

defined? 1 » “expression”
defined? dummy » nil
defined? printf » “method”
defined? String » “constant”
defined? $& » nil
defined? $_ » “global-variable”
defined? Math::PI » “constant”
defined? ( c,d = 1,2 ) » “assignment”

This one ^^^^^^^^^^^^^^^^ doesn’t seem to work under 1.8:

irb(main):002:0> defined?(c,d = 1,2)
SyntaxError: compile error
(irb):2: syntax error
defined?(c,d = 1,2)

defined? 42.abs » “method”

It’s interesting, I always thought defined? returned true or false, but it
returns a String (if the argument has been defined) or nil (if it hasn’t).
…you learn something new everyday (or should anyway).

Phil

···

Brian Candler B.Candler@pobox.com wrote:

On Wed, May 07, 2003 at 06:23:06AM +0900, Gennady wrote:

Saluton!

irb(main):002:0> defined?(c,d = 1,2)
SyntaxError: compile error
(irb):2: syntax error
defined?(c,d = 1,2)

Strange. The version of irb you use is which?

0:35:07 [5] /home/jupp> irb -v&&irb
irb 0.7.4(01/05/08)
irb(main):001:0> defined? ( c,d = 1,2 )
=> “assignment”

Gis,

Josef ‘Jupp’ Schugt

···


e-mails that do not contain plain text, are larger than 50 KiB, are
unsolicited, or contain binarys are ignored unless payment from your
side or technical reasons give rise to a non-standard treatment.

Saluton!

irb(main):002:0> defined?(c,d = 1,2)
SyntaxError: compile error
(irb):2: syntax error
defined?(c,d = 1,2)

Same for me:

ruby -v -e ‘defined?(a,b=1,2)’
ruby 1.8.0 (2003-03-10) [i386-freebsd4.6]
-e:1: parse error
defined?(a,b=1,2)
^
-e:1: parse error

···

On Wednesday, 7 May 2003 at 9:39:39 +0900, Josef ‘Jupp’ Schugt wrote:

Strange. The version of irb you use is which?

0:35:07 [5] /home/jupp> irb -v&&irb
irb 0.7.4(01/05/08)
irb(main):001:0> defined? ( c,d = 1,2 )
=> “assignment”


Jim Freeze

When all other means of communication fail, try words.

from 1.8.0p2:

$ irb18 -v
irb 0.9(02/07/03)
$ irb18
irb(main):001:0> defined? (a=1)
=> “assignment”
irb(main):002:0> defined? (a,b=1,2)
SyntaxError: compile error
(irb):2: syntax error
defined? (a,b=1,2)
^
(irb):2: syntax error
from (irb):2

···

On Wed, May 07, 2003 at 09:39:39AM +0900, Josef ‘Jupp’ Schugt wrote:

Strange. The version of irb you use is which?

0:35:07 [5] /home/jupp> irb -v&&irb
irb 0.7.4(01/05/08)
irb(main):001:0> defined? ( c,d = 1,2 )
=> “assignment”

I don’t have any 1.8 stuff yet, but is there a difference between : defined?
(a,b = 1,2)
and: defined?(a,b = 1,2) # note space

With all the poetry mode (IMO) nonsense and changes lately, anything
unexpected I’ve seen I’ve been afraid of whitespace causing it…

···

On Wed, May 07, 2003 at 09:39:39AM +0900, Josef ‘Jupp’ Schugt wrote:

Strange. The version of irb you use is which?

0:35:07 [5] /home/jupp> irb -v&&irb
irb 0.7.4(01/05/08)
irb(main):001:0> defined? ( c,d = 1,2 )
=> “assignment”

from 1.8.0p2:

$ irb18 -v
irb 0.9(02/07/03)
$ irb18
irb(main):001:0> defined? (a=1)
=> “assignment”
irb(main):002:0> defined? (a,b=1,2)
SyntaxError: compile error
(irb):2: syntax error
defined? (a,b=1,2)
^
(irb):2: syntax error
from (irb):2

Spaces don’t seem to matter:

ruby -v -e ‘defined?(a,b = 1,2)’
ruby 1.8.0 (2003-04-10) [sparc-solaris2.8]
-e:1: parse error
defined?(a,b = 1,2)
^
-e:1: parse error

···

On Wednesday, 7 May 2003 at 21:20:39 +0900, Mike Campbell wrote:

$ irb18 -v
irb 0.9(02/07/03)
$ irb18
irb(main):001:0> defined? (a=1)
=> “assignment”
irb(main):002:0> defined? (a,b=1,2)
SyntaxError: compile error
(irb):2: syntax error
defined? (a,b=1,2)
^
(irb):2: syntax error
from (irb):2

I don’t have any 1.8 stuff yet, but is there a difference between : defined?
(a,b = 1,2)
and: defined?(a,b = 1,2) # note space

With all the poetry mode (IMO) nonsense and changes lately, anything
unexpected I’ve seen I’ve been afraid of whitespace causing it…


Jim Freeze

… the MYSTERIANS are in here with my CORDUROY SOAP DISH!!

Hi,

···

In message “Re: Testing for a class existence” on 03/05/07, Jim Freeze jim@freeze.org writes:

Spaces don’t seem to matter:

Double parentheses will do.

ruby -v -e ‘p defined?((a,b=1,2))’
ruby 1.8.0 (2003-05-06) [i686-linux]
“assignment”

Multiple assignments can not appear in argument lists.

						matz.

In article 1052319883.385720.937.nullmailer@picachu.netlab.jp,

···

Yukihiro Matsumoto matz@ruby-lang.org wrote:

Hi,

In message “Re: Testing for a class existence” > on 03/05/07, Jim Freeze jim@freeze.org writes:

Spaces don’t seem to matter:

Double parentheses will do.

ruby -v -e ‘p defined?((a,b=1,2))’
ruby 1.8.0 (2003-05-06) [i686-linux]
“assignment”

Multiple assignments can not appear in argument lists.

  					matz.

This is a change in 1.8, right? It apparently used to work in 1.6.x.

What was the reason for the change?

Phil

Hi,

···

In message “Re: Testing for a class existence” on 03/05/08, Phil Tomson ptkwt@shell1.aracnet.com writes:

This is a change in 1.8, right? It apparently used to work in 1.6.x.

What was the reason for the change?

Yes. It’s to be more consistent with other function calls.

						matz.

I just check my hardcopy of Pickaxe again. On page 221 there’s a precedence
table, where “defined?” is checked as a method (in addition to other spots
in the book I wrote about before). So, shell we admit that this is a mistake
or was there a time when “define?” was actually a method?

Gennady.

···

----- Original Message -----
From: “Yukihiro Matsumoto” matz@ruby-lang.org
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Wednesday, May 07, 2003 6:41 PM
Subject: Re: Testing for a class existence

Hi,

In message “Re: Testing for a class existence” > on 03/05/08, Phil Tomson ptkwt@shell1.aracnet.com writes:

This is a change in 1.8, right? It apparently used to work in 1.6.x.

What was the reason for the change?

Yes. It’s to be more consistent with other function calls.

matz.