Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Verify if a class exists

Reply
Thread Tools

Verify if a class exists

 
 
Bernd
Guest
Posts: n/a
 
      07-04-2007
Hello everybody,

is there a way to check, if a class exists? I create classes dynamically
and would like to know before, if a class with that name already exists.

Thanks for your help
Bernd

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Simen Edvardsen
Guest
Posts: n/a
 
      07-04-2007
On 7/4/07, Bernd <> wrote:
> Hello everybody,
>
> is there a way to check, if a class exists? I create classes dynamically
> and would like to know before, if a class with that name already exists.
>
> Thanks for your help
> Bernd
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


Try a const_get. Like so:

def class_exists?(name)
begin
true if Kernel.const_get(name)
rescue NameError
false
end
end

--
- Simen

 
Reply With Quote
 
 
 
 
Bernd Burnt
Guest
Posts: n/a
 
      07-04-2007
Simen Edvardsen wrote:
> On 7/4/07, Bernd <> wrote:
>>
>>

>
> Try a const_get. Like so:
>
> def class_exists?(name)
> begin
> true if Kernel.const_get(name)
> rescue NameError
> false
> end
> end


Hi,
first of all, thanks for your help, Simen. I think, I even found a
better solution, I assign the name to the class with

Object.const_set class_name, klass

So I can determine, whether the class exists with

Object const_defined? class_name

You are using exception handling (rescue) for a conditional branch,
which I try to avoid whenever possible, to keep my code well structured.

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Simen Edvardsen
Guest
Posts: n/a
 
      07-04-2007
On 7/4/07, Bernd Burnt <> wrote:
> Simen Edvardsen wrote:
> > On 7/4/07, Bernd <> wrote:
> >>
> >>

> >
> > Try a const_get. Like so:
> >
> > def class_exists?(name)
> > begin
> > true if Kernel.const_get(name)
> > rescue NameError
> > false
> > end
> > end

>
> Hi,
> first of all, thanks for your help, Simen. I think, I even found a
> better solution, I assign the name to the class with
>
> Object.const_set class_name, klass
>
> So I can determine, whether the class exists with
>
> Object const_defined? class_name
>
> You are using exception handling (rescue) for a conditional branch,
> which I try to avoid whenever possible, to keep my code well structured.
>


There is a const_defined? method? Ah, I see it's been some time since
I programmed Ruby. Excuse my ignorant advice, it seems you find a
better solution

> --
> Posted via http://www.ruby-forum.com/.
>
>



--
- Simen

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to verify file exists with asp:FileUpload bryanp10@hotmail.com ASP .Net 4 03-30-2007 10:22 PM
Verify if Session(hashtable) exists Carlos Albert ASP .Net 8 12-14-2005 08:13 PM
Verify that line exists in a file Jonny C Programming 9 02-20-2005 05:55 PM
Verify File Exists =?Utf-8?B?TXJNaWtl?= ASP .Net 2 01-05-2005 04:17 PM
Verify if file exists on network shared HELP Henry Miranda ASP .Net Security 0 12-17-2003 06:21 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57