# Given a string find the matching class

**URL:** <https://www.ruby-forum.com/t/given-a-string-find-the-matching-class/155263>\
**Category:** Ruby\
**Created:** [December 9, 2008, 11:13pm UTC](https://www.ruby-forum.com/t/given-a-string-find-the-matching-class/155263 "2008-12-09T23:13:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [December 9, 2008, 11:13pm UTC](https://www.ruby-forum.com/t/given-a-string-find-the-matching-class/155263/1 "2008-12-09T23:13:22Z")

</div>

I have a situation where I desire to pass a string to a method that  
then:

1. Capitalizes the the string value

2. Finds a class by that name

Something like:

def findmyclass(m)  
find\_class\_by\_name(m.capitalize)  
end

Is this possible (surely) and if so, how is it done?

Thanks

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [December 10, 2008, 2:12am UTC](https://www.ruby-forum.com/t/given-a-string-find-the-matching-class/155263/2 "2008-12-10T02:12:21Z")

</div>

On Wed, Dec 10, 2008 at 6:06 AM, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have a situation where I desire to pass a string to a method that  
> then:
> 
> 1. Capitalizes the the string value
> 2. Finds a class by that name  
> Something like:  
> def findmyclass(m)  
> find\_class\_by\_name(m.capitalize)  
> end  
> Is this possible (surely) and if so, how is it done?

1 class names are constants, thus  
1.1 they start w upcase, and  
1.2 are case sensitive

2 you get the value of a quoted/stringified constant  
by using const\_get

3 you can then check the value returned by checking if it #is\_a? Class

eg,

> Object.const\_get(“XX”).is\_a? Class  
> =\> true  
> Object.const\_get(“Xx”).is\_a? Class  
> =\> true  
> Object.const\_get(“YY”)  
> NameError: uninitialized constant YY

now, if you want to find classes tucked in on other modules/classes,  
you’ll have to scan the whole ObjectSpace; eg, looking for a class  
named “C” wc is in “A:🅱:C”…

eg,

> ObjectSpace.each\_object(Class).each {|x| p x if x.name =~ /^M/ }  
> Method  
> MatchData  
> Module  
> M::MM

---

<div class="post-metadata">

**Author:** ![byrnejb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/d2c977/32.png) [@byrnejb](https://www.ruby-forum.com/u/byrnejb)\
**Post date:** [December 10, 2008, 9:07pm UTC](https://www.ruby-forum.com/t/given-a-string-find-the-matching-class/155263/3 "2008-12-10T21:07:54Z")

</div>

botp wrote:

> 3 you can then check the value returned by checking if it #is\_a? Class  
> …
> 
> > Object.const\_get(“XX”).is\_a? Class  
> > =\> true  
> > Object.const\_get(“Xx”).is\_a? Class  
> > =\> true  
> > Object.const\_get(“YY”)  
> > NameError: uninitialized constant YY
> 
> now, if you want to find classes tucked in on other modules/classes,  
> you’ll have to scan the whole ObjectSpace; eg, looking for a class  
> named “C” wc is in “A:🅱:C”…  
> …  
> ObjectSpace.each\_object(Class).each {|x| p x if x.name =~ /^M/ }  
> Method  
> MatchData  
> Module  
> M::MM

Many thanks for this lucid explanation.
