Getting class name without module

Discussion in 'Ruby' started by Jari Williamsson, Feb 12, 2008.

  1. 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
     
    Jari Williamsson, Feb 12, 2008
    #1
    1. Advertising

  2. Jari Williamsson

    Xavier Noria Guest

    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
     
    Xavier Noria, Feb 12, 2008
    #2
    1. Advertising

  3. Jari Williamsson

    Guest

    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"
     
    , Dec 4, 2012
    #3
  4. 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
     
    Robert Klemme, Dec 5, 2012
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilariu Raducan
    Replies:
    2
    Views:
    361
    Ilariu Raducan
    Jul 14, 2004
  2. Patrick

    Getting windows user-name without domain name

    Patrick, Dec 7, 2007, in forum: ASP .Net Security
    Replies:
    2
    Views:
    589
    Nikolay Podkolzin
    Dec 14, 2007
  3. Mike Austin
    Replies:
    1
    Views:
    112
  4. Replies:
    2
    Views:
    114
  5. Iñaki Baz Castillo
    Replies:
    6
    Views:
    180
    Iñaki Baz Castillo
    Mar 31, 2009
Loading...

Share This Page