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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With