Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Is this potentially a problem?

Reply
Thread Tools

Is this potentially a problem?

 
 
DaZoner
Guest
Posts: n/a
 
      08-11-2004

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


 
Reply With Quote
 
 
 
 
Phil Tomson
Guest
Posts: n/a
 
      08-11-2004
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
 
Reply With Quote
 
 
 
 
George Ogata
Guest
Posts: n/a
 
      08-11-2004
"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:::E (in that case you'd need to call C:.const_defined?('E'),
e.g.).
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-12-2004

"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:::E (in that case you'd need to call C:.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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A potentially dangerous querystring ... [ValidateRequest] Boris ASP .Net 5 04-17-2004 05:22 PM
A potentially dangerous Request.Form value was detected from the client amit ASP .Net 1 02-26-2004 09:47 PM
Why Getting 'A Potentially Dangerous Request...' Error? Anil Kripalani ASP .Net 2 02-25-2004 06:39 PM
A potentially dangerous Request.Form Alex Munk ASP .Net 2 12-17-2003 09:11 AM
Potentially Massive Internet Attack Starts Today =?iso-8859-1?Q?Frisbee=AE_MCNGP?= MCSE 14 08-26-2003 02:12 AM



Advertisments