Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Getting class name without module

Reply
Thread Tools

Getting class name without module

 
 
Jari Williamsson
Guest
Posts: n/a
 
      02-12-2008
Is there a method available to get only the class name, without getting
the module name prior to it? Alternatively, to get the class name within
the current name space context?

For example, self.class could return something like this:
MyModule::MyClass
(I would like to only get the "MyClass" part.)

Or do I have to resort to string manipulation/regexps?


Best regards,

Jari Williamsson

 
Reply With Quote
 
 
 
 
Xavier Noria
Guest
Posts: n/a
 
      02-12-2008
On Feb 12, 2008, at 13:05 , Jari Williamsson wrote:

> Is there a method available to get only the class name, without
> getting the module name prior to it? Alternatively, to get the class
> name within the current name space context?
>
> For example, self.class could return something like this:
> MyModule::MyClass
> (I would like to only get the "MyClass" part.)
>
> Or do I have to resort to string manipulation/regexps?


Indeed, you do it by hand, something like this:

name.split('::').last || ''

-- fxn


 
Reply With Quote
 
 
 
 
irio.musskopf@caixadeideias.com.br
Guest
Posts: n/a
 
      12-04-2012
Em terça-feira, 12 de fevereiro de 2008 10h05min31s UTC-2, Jari Williamsson escreveu:
> Is there a method available to get only the class name, without getting
> the module name prior to it? Alternatively, to get the class name within
> the current name space context?
>
> For example, self.class could return something like this:
> MyModule::MyClass
> (I would like to only get the "MyClass" part.)
>
> Or do I have to resort to string manipulation/regexps?
>
>
> Best regards,
>
> Jari Williamsson




Em terça-feira, 12 de fevereiro de 2008 10h05min31s UTC-2, Jari Williamsson escreveu:
> Is there a method available to get only the class name, without getting
> the module name prior to it? Alternatively, to get the class name within
> the current name space context?
>
> For example, self.class could return something like this:
> MyModule::MyClass
> (I would like to only get the "MyClass" part.)
>
> Or do I have to resort to string manipulation/regexps?
>
>
> Best regards,
>
> Jari Williamsson


If you are using ActiveSupport:

MyModule::MyClass.to_s.demodulize # => "MyClass"
 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      12-05-2012
On 12/04/2012 08:24 PM, wrote:
> Em terça-feira, 12 de fevereiro de 2008 10h05min31s UTC-2, Jari Williamsson escreveu:
>> Is there a method available to get only the class name, without getting
>> the module name prior to it? Alternatively, to get the class name within
>> the current name space context?
>>
>> For example, self.class could return something like this:
>> MyModule::MyClass
>> (I would like to only get the "MyClass" part.)
>>
>> Or do I have to resort to string manipulation/regexps?


irb(main):001:0> module X; class Y; end end
=> nil
irb(main):002:0> X::Y.name
=> "X::Y"
irb(main):003:0> X::Y.name.split(/::/).last
=> "Y"
irb(main):004:0> X::Y.name.split('::').last
=> "Y"

Cheers

robert

 
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 the current module(s), class name and method in Ruby 1.9 Iñaki Baz Castillo Ruby 6 03-31-2009 07:55 PM
Getting windows user-name without domain name Patrick ASP .Net Security 2 12-14-2007 04:23 PM
module name == class name? aidy.lewis@googlemail.com Ruby 2 07-11-2007 09:50 PM
Getting the name of a class that's in a module Mike Austin Ruby 1 03-17-2006 10:11 PM
Getting module object by name in C extension module Ilariu Raducan Python 2 07-14-2004 10:56 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57