Is this potentially a problem?

Discussion in 'Ruby' started by DaZoner, Aug 11, 2004.

  1. DaZoner

    DaZoner Guest

    I've defined a class I use that strips methods off of a class (specified by
    a string representing the class name). I use it maybe 30 times during my
    application run. However I'm concerned because it does an
    ObjectSpace.each(Class) call to make sure the specified class exists before
    trying to remove any methods. Will this call get slow as a few arrays
    containing perhaps a million FixNums get defined by the application in
    between calls? The array's data stays around as the calls to my class are
    made. I've included the source of the class below. If anyone knows an
    efficient way to determine if a class exists given its name in String form
    I'd sure appreciate seeing it.

    class MethodRemover
    def initialize(className)
    @className = className
    @codeString = "class " + className +"\n" +
    " public_instance_methods(false).each do | method |\n" +
    " code = \"class #{className} remove_method
    :\"\+method\+\" end\"\n" +
    " Object.module_eval(code)\n" +
    " end\n" +
    "end\n"
    end
    def run
    #print(@codeString)
    ObjectSpace.each_object(Class) do | aClass |
    if aClass.to_s == @className
    eval(@codeString)
    end
    end
    end
    end
    DaZoner, Aug 11, 2004
    #1
    1. Advertising

  2. DaZoner

    Phil Tomson Guest

    In article <cfe1i1$126$>,
    DaZoner <> wrote:
    >
    >I've defined a class I use that strips methods off of a class (specified by
    >a string representing the class name). I use it maybe 30 times during my
    >application run. However I'm concerned because it does an
    >ObjectSpace.each(Class) call to make sure the specified class exists before
    >trying to remove any methods. Will this call get slow as a few arrays
    >containing perhaps a million FixNums get defined by the application in
    >between calls?


    Since you specified Class as the argument to ObjectSpace.each it shouln't
    be including Fixnums in the list that gets iterated - only classes.

    >The array's data stays around as the calls to my class are
    >made. I've included the source of the class below. If anyone knows an
    >efficient way to determine if a class exists given its name in String form
    >I'd sure appreciate seeing it.
    >


    I'm not sure if it would be faster (you'll have to experiment) but maybe
    'defined?' would work.

    if defined? @classname



    Phil
    Phil Tomson, Aug 11, 2004
    #2
    1. Advertising

  3. DaZoner

    George Ogata Guest

    "DaZoner" <> writes:

    > I've defined a class I use that strips methods off of a class (specified by
    > a string representing the class name). I use it maybe 30 times during my
    > application run. However I'm concerned because it does an
    > ObjectSpace.each(Class) call to make sure the specified class exists before
    > trying to remove any methods. Will this call get slow as a few arrays
    > containing perhaps a million FixNums get defined by the application in
    > between calls? The array's data stays around as the calls to my class are
    > made. I've included the source of the class below. If anyone knows an
    > efficient way to determine if a class exists given its name in String form
    > I'd sure appreciate seeing it.
    >


    Something like:

    Object.const_defined?(name) && Object.const_get(name).is_a?(Class)

    You'd need to tweak it a bit to support nested class names like
    C::D::E (in that case you'd need to call C::D.const_defined?('E'),
    e.g.).
    George Ogata, Aug 11, 2004
    #3
  4. "George Ogata" <> schrieb im Newsbeitrag
    news:...
    > "DaZoner" <> writes:
    >
    > > I've defined a class I use that strips methods off of a class

    (specified by
    > > a string representing the class name). I use it maybe 30 times during

    my
    > > application run. However I'm concerned because it does an
    > > ObjectSpace.each(Class) call to make sure the specified class exists

    before
    > > trying to remove any methods. Will this call get slow as a few arrays
    > > containing perhaps a million FixNums get defined by the application in
    > > between calls? The array's data stays around as the calls to my class

    are
    > > made. I've included the source of the class below. If anyone knows an
    > > efficient way to determine if a class exists given its name in String

    form
    > > I'd sure appreciate seeing it.
    > >

    >
    > Something like:
    >
    > Object.const_defined?(name) && Object.const_get(name).is_a?(Class)
    >
    > You'd need to tweak it a bit to support nested class names like
    > C::D::E (in that case you'd need to call C::D.const_defined?('E'),
    > e.g.).


    For example like this:

    module Kernel
    private

    # raises NameError if it's undefined
    # raises TypeError if it's not a class
    # returns the class instance otherwise
    def get_class(class_name)
    c = class_name.split(/::|\./).inject(Object) do |cl, name|
    cl.const_get name
    end
    raise TypeError, "Not a class: #{class_name}" unless Class === c
    c
    end
    end

    >> get_class "String"

    => String
    >> get_class "StringX"

    NameError: uninitialized constant StringX
    from (irb):27:in `const_get'
    from (irb):27:in `get_class'
    from (irb):26:in `inject'
    from (irb):26:in `each'
    from (irb):26:in `inject'
    from (irb):26:in `get_class'
    from (irb):35
    >> get_class "File::Stat"

    => File::Stat
    >> get_class "Enumerable"

    TypeError: Not a class: Enumerable
    from (irb):29:in `get_class'
    from (irb):37
    >>


    You probable want to include modules as well. Just change to

    raise TypeError, "Not a class: #{class_name}" unless Class === c ||
    Module === c

    >> get_class "Enumerable"

    => Enumerable

    Regards

    robert
    Robert Klemme, Aug 12, 2004
    #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. Alex Munk

    A potentially dangerous Request.Form

    Alex Munk, Dec 16, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    574
    Adrijan Josic
    Dec 17, 2003
  2. Anil Kripalani
    Replies:
    2
    Views:
    476
    Eric Lawrence [MSFT]
    Feb 25, 2004
  3. amit
    Replies:
    1
    Views:
    501
    Eric Lawrence [MSFT]
    Feb 26, 2004
  4. Boris
    Replies:
    5
    Views:
    2,519
    Joe Kaplan \(MVP - ADSI\)
    Apr 17, 2004
  5. John Morgan
    Replies:
    1
    Views:
    2,941
    Oleg Ogurok
    May 27, 2004
Loading...

Share This Page