John Smith
John Smith

Reputation: 6259

Get name of subclass

Lets say I have I class called

a = Person::User::Base

How can I get only the last subclass called Base.

The way i know how to do this is:

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

=> "Base" 

Is there a better way?

Upvotes: 9

Views: 1287

Answers (1)

Brian Knight
Brian Knight

Reputation: 5041

If you use Rails (ActiveSupport):

a.to_s.demodulize

If you use POR (plain-ol-Ruby), yes, it's your way:

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

Upvotes: 12

Related Questions