Forum: Ruby Getting the name of a class that's in a module

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
06db81f02537377c4bb207cdf8fd18ec?d=identicon&s=25 Mike Austin (Guest)
on 2006-03-17 22:30
(Received via mailing list)
'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
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-17 23:12
(Received via mailing list)
On Sat, 18 Mar 2006, Mike Austin 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
This topic is locked and can not be replied to.