# Getting the name of a class that's in a module

**URL:** <https://www.ruby-forum.com/t/getting-the-name-of-a-class-thats-in-a-module/55206>\
**Category:** Ruby\
**Created:** [March 17, 2006, 10:30pm UTC](https://www.ruby-forum.com/t/getting-the-name-of-a-class-thats-in-a-module/55206 "2006-03-17T22:30:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![exile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/50afbb/32.png) [@exile](https://www.ruby-forum.com/u/exile)\
**Post date:** [March 17, 2006, 10:30pm UTC](https://www.ruby-forum.com/t/getting-the-name-of-a-class-thats-in-a-module/55206/1 "2006-03-17T22:30:20Z")

</div>

‘some\_object.class.to\_s’ doesn’t work because it returns the entire  
hierarchy,  
i.e ‘SomeModule::SomeClass’. I could do some string ops on that but it  
seems  
there would be an easier way. Here’s an example:

class MenuStyle  
class Button \< Style  
end  
end

class View  
def initialize()  
@delegate = MenuStyle.const\_get( self.class.to\_s )  
end  
end

Thanks,  
Mike

---

<div class="post-metadata">

**Author:** ![exile](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/50afbb/32.png) [@exile](https://www.ruby-forum.com/u/exile)\
**Post date:** [March 17, 2006, 11:12pm UTC](https://www.ruby-forum.com/t/getting-the-name-of-a-class-thats-in-a-module/55206/2 "2006-03-17T23:12:53Z")

</div>

On Sat, 18 Mar 2006, Mike A. wrote:

> def initialize()  
> @delegate = MenuStyle.const\_get( self.class.to\_s )  
> end  
> end

```
 harp:~ > cat a.rb
 #
 # creates a class by class name
 #
   def klass_stamp(hierachy, *a, &b)
     ancestors = hierachy.split(%r/::/)
     parent = Object
     while((child = ancestors.shift))
       klass = parent.const_get child
       parent = klass
     end
     klass::new(*a, &b)
   end

```

hth.

-a
