Verify if a class exists

Discussion in 'Ruby' started by Bernd, Jul 4, 2007.

  1. Bernd

    Bernd Guest

    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/.
    Bernd, Jul 4, 2007
    #1
    1. Advertising

  2. 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
    Simen Edvardsen, Jul 4, 2007
    #2
    1. Advertising

  3. Bernd

    Bernd Burnt Guest

    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/.
    Bernd Burnt, Jul 4, 2007
    #3
  4. 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
    Simen Edvardsen, Jul 4, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TXJNaWtl?=

    Verify File Exists

    =?Utf-8?B?TXJNaWtl?=, Jan 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    6,013
    =?Utf-8?B?TXJNaWtl?=
    Jan 5, 2005
  2. Carlos Albert

    Verify if Session(hashtable) exists

    Carlos Albert, Dec 9, 2005, in forum: ASP .Net
    Replies:
    8
    Views:
    6,381
    addup
    Dec 14, 2005
  3. Jonny

    Verify that line exists in a file

    Jonny, Feb 19, 2005, in forum: C Programming
    Replies:
    9
    Views:
    374
    Mark McIntyre
    Feb 20, 2005
  4. Replies:
    4
    Views:
    959
  5. Henry Miranda

    Verify if file exists on network shared HELP

    Henry Miranda, Dec 17, 2003, in forum: ASP .Net Security
    Replies:
    0
    Views:
    112
    Henry Miranda
    Dec 17, 2003
Loading...

Share This Page