Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Getting the name of a class that's in a module

Reply
Thread Tools

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

 
 
Mike Austin
Guest
Posts: n/a
 
      03-17-2006
'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
 
Reply With Quote
 
 
 
 
ara.t.howard@noaa.gov
Guest
Posts: n/a
 
      03-17-2006
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


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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting class name without module Jari Williamsson Ruby 3 12-05-2012 07:19 AM
Getting the current module(s), class name and method in Ruby 1.9 IƱaki Baz Castillo Ruby 6 03-31-2009 07:55 PM
module name == class name? aidy.lewis@googlemail.com Ruby 2 07-11-2007 09:50 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Getting module object by name in C extension module Ilariu Raducan Python 2 07-14-2004 10:56 AM



Advertisments