Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Is this potentially a problem? (http://www.velocityreviews.com/forums/t816086-is-this-potentially-a-problem.html)

DaZoner 08-11-2004 09:02 PM

Is this potentially a problem?
 

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



Phil Tomson 08-11-2004 09:34 PM

Re: Is this potentially a problem?
 
In article <cfe1i1$126$1@news.doit.wisc.edu>,
DaZoner <nobody@nowhere.com> 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

George Ogata 08-11-2004 10:36 PM

Re: Is this potentially a problem?
 
"DaZoner" <nobody@nowhere.com> 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.).

Robert Klemme 08-12-2004 07:12 AM

Re: Is this potentially a problem?
 

"George Ogata" <g_ogata@optushome.com.au> schrieb im Newsbeitrag
news:87d61xwbct.fsf@optushome.com.au...
> "DaZoner" <nobody@nowhere.com> 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



All times are GMT. The time now is 02:29 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.