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

'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

harp:~ > cat a.rb

···

On Sat, 18 Mar 2006, Mike Austin wrote:

'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

     #
     # 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
--
share your knowledge. it's a way to achieve immortality.
- h.h. the 14th dali lama