Is this potentially a problem?

D

DaZoner

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
 
P

Phil Tomson

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
 
G

George Ogata

DaZoner said:
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.).
 
R

Robert Klemme

George Ogata said:
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
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):35TypeError: 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
=> Enumerable

Regards

robert
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top