Get subclass name

Let's say I have a class called

a = Person::User::Base 

How can I get only the last subclass called Base .

How do I know how to do this:

 a.to_s.split('::').last => "Base" 

Is there a better way?

+7
ruby ruby-on-rails
source share
1 answer

If you are using Rails (ActiveSupport):

a.to_s.demodulize

If you use POR (plain-ol-Ruby), yes, this is your way:

a.to_s.split('::').last

+7
source share

All Articles