Professional Web Applications Themes

check whether a class is defined, listing all classes - Ruby

Given a string of fully-qualified class name, how do I easily check whether the class is defined or not? I hope it's not as 'hard' as I think: def class_defined?(classname) m = Class classname.split("::").each { |c| m.constants.include?(c) or return false m = m.const_get c } return true end p class_defined?("Test::Unit::TestCase") # false require 'test/unit/testcase' p class_defined?("Test::Unit::TestCase") # true Is there an easier way? A bonus question, how do I list all the defined classes in their fully qualified name? Example output: Object String FalseClass Struct Struct::Tms File File::Stat ... (My own code is very ugly, I'm embarrassed to show it ...

  1. #1

    Default check whether a class is defined, listing all classes

    Given a string of fully-qualified class name, how do I easily check
    whether the class is defined or not? I hope it's not as 'hard' as I think:

    def class_defined?(classname)
    m = Class
    classname.split("::").each { |c|
    m.constants.include?(c) or return false
    m = m.const_get c
    }
    return true
    end

    p class_defined?("Test::Unit::TestCase") # false
    require 'test/unit/testcase'
    p class_defined?("Test::Unit::TestCase") # true

    Is there an easier way?

    A bonus question, how do I list all the defined classes in their fully
    qualified name? Example output:

    Object
    String
    FalseClass
    Struct
    Struct::Tms
    File
    File::Stat
    ...

    (My own code is very ugly, I'm embarrassed to show it :-)

    --
    dave



    David Guest

  2. #2

    Default Re: check whether a class is defined, listing all classes


    "David Garamond" <6.isreserved.com> schrieb im Newsbeitrag
    news:6.isreserved.com... 

    irb(main):009:0> defined? Test::Unit::TestCase
    => nil
    irb(main):010:0> require 'test/unit/testcase'
    => true
    irb(main):011:0> defined? Test::Unit::TestCase
    => "constant"
    irb(main):012:0>
     

    ObjectSpace.each_object( Class ){|cl| p cl}
    ObjectSpace.each_object( Class ){|cl| p cl.name}

    Regards

    robert

    Robert Guest

  3. #3

    Default Re: check whether a class is defined, listing all classes

    Robert Klemme wrote: 
    >
    > irb(main):009:0> defined? Test::Unit::TestCase
    > => nil
    > irb(main):010:0> require 'test/unit/testcase'
    > => true
    > irb(main):011:0> defined? Test::Unit::TestCase
    > => "constant"[/ref]

    But what if the class name to be checked is a string?
    ("Test::Unit::TestCase")? Btw, is "defined?" a method (of what class?)?
    A statement?
     
    >
    > ObjectSpace.each_object( Class ){|cl| p cl}
    > ObjectSpace.each_object( Class ){|cl| p cl.name}[/ref]

    Ah, Class#name... Thanks, I'm so glad it's straightforward & easy.

    --
    dave



    David Guest

  4. #4

    Default Re: check whether a class is defined, listing all classes


    "David Garamond" <6.isreserved.com> schrieb im Newsbeitrag
    news:6.isreserved.com... 
    > >
    > > irb(main):009:0> defined? Test::Unit::TestCase
    > > => nil
    > > irb(main):010:0> require 'test/unit/testcase'
    > > => true
    > > irb(main):011:0> defined? Test::Unit::TestCase
    > > => "constant"[/ref]
    >
    > But what if the class name to be checked is a string?
    > ("Test::Unit::TestCase")?[/ref]

    irb(main):005:0> eval "defined? " + "Test::Unit::TestCase"
    => nil
    irb(main):006:0> require 'test/unit/testcase'
    => true
    irb(main):007:0> eval "defined? " + "Test::Unit::TestCase"
    => "constant"

     

    It's an operator:
    http://www.rubycentral.com/book/tut_expressions.html
     
    > >
    > > ObjectSpace.each_object( Class ){|cl| p cl}
    > > ObjectSpace.each_object( Class ){|cl| p cl.name}[/ref]
    >
    > Ah, Class#name... Thanks, I'm so glad it's straightforward & easy.[/ref]


    :-)

    robert

    Robert Guest

  5. #5

    Default Re: check whether a class is defined, listing all classes

    Robert Klemme wrote: 
    >
    > irb(main):005:0> eval "defined? " + "Test::Unit::TestCase"
    > => nil
    > irb(main):006:0> require 'test/unit/testcase'
    > => true
    > irb(main):007:0> eval "defined? " + "Test::Unit::TestCase"
    > => "constant"[/ref]

    Hm, I should've added that I didn't want to involve 'eval'. But then why
    not, I'll just need to check the input using a simple regexp. After all,
    almost everything in Ruby (require, load, irb, class_eval) is using eval
    anyway...

    Thanks again.

    --
    dave



    David Guest

  6. #6

    Default Re: check whether a class is defined, listing all classes

    >>>>> "D" == David Garamond <6.isreserved.com> writes:

    D> Hm, I should've added that I didn't want to involve 'eval'. But then why
    D> not, I'll just need to check the input using a simple regexp.

    well, if you think that a simple regexp can do anything against the evil
    eval, then you have perhaps a P background :-)


    Guy Decoux





    ts Guest

  7. #7

    Default Re: check whether a class is defined, listing all classes

    On Monday, February 2, 2004, 12:52:30 AM, David wrote:
     
    >>
    >> irb(main):005:0> eval "defined? " + "Test::Unit::TestCase"
    >> => nil
    >> irb(main):006:0> require 'test/unit/testcase'
    >> => true
    >> irb(main):007:0> eval "defined? " + "Test::Unit::TestCase"
    >> => "constant"[/ref][/ref]
     

    Take a look at StandardClastensions on the Wiki. There's something
    in there just for you :)

    Cheers,
    Gavin



    Gavin Guest

  8. #8

    Default Re: check whether a class is defined, listing all classes

    ts wrote: [/ref]
    > D> Hm, I should've added that I didn't want to involve 'eval'. But then why
    > D> not, I'll just need to check the input using a simple regexp.
    >
    > well, if you think that a simple regexp can do anything against the evil
    > eval, then you have perhaps a P background :-)[/ref]

    Don't you also have a P background? :) (Or Matz? Or many others?)

    http://www.google.com/search?num=100&q=guy+decoux+perl

    Anyway, I didn't say regexp can make eval safe generally, but only for
    my case:

    def class_defined?(classname)
    classname.kind_of? String or
    raise ArgumentError, "please give me string"
    classname =~ /\A[A-Z][A-Za-z0-9_]*(::[A-Z][A-Za-z0-9_]*)*\z/ or
    raise ArgumentError, "invalid class name"
    eval("defined? " + classname) != nil
    end

    Please do tell me if the above is unsafe...

    --
    dave



    David Guest

  9. #9

    Default Re: check whether a class is defined, listing all classes


    "David Garamond" <6.isreserved.com> schrieb im Newsbeitrag
    news:6.isreserved.com... 
    > > D> Hm, I should've added that I didn't want to involve 'eval'. But then[/ref][/ref]
    why [/ref]
    evil 
    >
    > Don't you also have a P background? :) (Or Matz? Or many others?)
    >
    > http://www.google.com/search?num=100&q=guy+decoux+perl
    >
    > Anyway, I didn't say regexp can make eval safe generally, but only for
    > my case:
    >
    > def class_defined?(classname)
    > classname.kind_of? String or
    > raise ArgumentError, "please give me string"
    > classname =~ /\A[A-Z][A-Za-z0-9_]*(::[A-Z][A-Za-z0-9_]*)*\z/ or
    > raise ArgumentError, "invalid class name"
    > eval("defined? " + classname) != nil[/ref]

    You don't need to compare with nil, that's converting something to a boolean
    that is essentially a boolean already - unless, of course, if you want to
    hide the outcome of "defined?"...

    To make the methods semantic match the name you should do

    eval("defined? " + classname) && Class === eval(classname)

    Because otherwise you will get true for all sorts of constants. (Try with
    "IO::CREAT")
     

    Looks safe enough.

    robert

    Robert Guest

Similar Threads

  1. Variable is defined check from URL????
    By Clumsy Hamster in forum Macromedia ColdFusion
    Replies: 6
    Last Post: February 23rd, 01:52 AM
  2. Classes defined in ASP pages -- desirable?
    By Me in forum ASP Components
    Replies: 4
    Last Post: May 3rd, 06:51 AM
  3. List of defined classes/modules
    By Mark in forum Ruby
    Replies: 5
    Last Post: January 19th, 12:56 PM
  4. Check out this Ebay listing . . .
    By Matt Clara in forum Photography
    Replies: 6
    Last Post: July 13th, 09:12 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139