Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 486
John Smith Avatar asked May 14 '15 19:05

John Smith


People also ask

How to get the name of the subclass in Java?

The method getClass(), inherited by every class from java. lang. Object, returns the Class object representing the object on which the method is called. The Class object has a getName() method that returns the name of the class.

How to get subclass in Python?

If you do have a string representing the name of a class and you want to find that class's subclasses, then there are two steps: find the class given its name, and then find the subclasses with __subclasses__ as above. However you find the class, cls. __subclasses__() would then return a list of its subclasses.

How do I find parent class names?

The parent() method is used to return all ancestor of the selected elements. Check if ancestor (parent) class exist then it returns the class name otherwise returns not exist.

What is a qualified name in Java?

A fully-qualified class name in Java contains the package that the class originated from. An example of this is java. util. ArrayList. The fully-qualified class name can be obtained using the getName() method.


1 Answers

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

like image 170
Brian Knight Avatar answered Oct 03 '22 20:10

Brian Knight