# Is this potentially a problem?

**URL:** <https://rubytalk.org/t/is-this-potentially-a-problem/13004>\
**Category:** ruby-talk\
**Created:** [11 August 2004 22:51 UTC](https://rubytalk.org/t/is-this-potentially-a-problem/13004 "2004-08-11T22:51:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![George\_Ogata1](https://avatars.discourse-cdn.com/v4/letter/g/d9b06d/32.png) [@George\_Ogata1](https://rubytalk.org/u/George_Ogata1)\
**Post date:** [11 August 2004 22:51 UTC](https://rubytalk.org/t/is-this-potentially-a-problem/13004/1 "2004-08-11T22:51:13Z")

</div>

"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:

&nbsp;&nbsp;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.).

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [12 August 2004 07:16 UTC](https://rubytalk.org/t/is-this-potentially-a-problem/13004/2 "2004-08-12T07:16:11Z")

</div>

"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:
> 
> &nbsp;&nbsp;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  
&nbsp;&nbsp;private

&nbsp;&nbsp;# raises NameError if it's undefined  
&nbsp;&nbsp;# raises TypeError if it's not a class  
&nbsp;&nbsp;# returns the class instance otherwise  
&nbsp;&nbsp;def get\_class(class\_name)  
&nbsp;&nbsp;&nbsp;&nbsp;c = class\_name.split(/::|\./).inject(Object) do |cl, name|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cl.const\_get name  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;raise TypeError, "Not a class: #{class\_name}" unless Class === c  
&nbsp;&nbsp;&nbsp;&nbsp;c  
&nbsp;&nbsp;end  
end

> > get\_class "String"

=\> String

> > get\_class "StringX"

NameError: uninitialized constant StringX  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):27:in `const\_get'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):27:in `get\_class'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):26:in `inject'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):26:in `each'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):26:in `inject'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):26:in `get\_class'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):35

> > get\_class "File::Stat"

=\> File::Stat

> > get\_class "Enumerable"

TypeError: Not a class: Enumerable  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):29:in `get\_class'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):37

> >

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

&nbsp;&nbsp;&nbsp;&nbsp;raise TypeError, "Not a class: #{class\_name}" unless Class === c ||  
Module === c

> > get\_class "Enumerable"

=\> Enumerable

Regards

&nbsp;&nbsp;&nbsp;&nbsp;robert
