Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get only class name without namespace

There is a class like this.

module Foo   class Bar   end end 

And I want to get the class name of Bar without Foo.

bar = Foo::Bar.new bar.class.to_s.match('::(.+)$'){ $1 } 

I could get the class name by this code, but I don't think this is a best way to get it.

Is there better way to get the name of class without namespace?

like image 915
ironsand Avatar asked Oct 31 '16 05:10

ironsand


1 Answers

If you are using Rails, you can actually use the demodulize method on the String class. http://apidock.com/rails/String/demodulize

bar.class.name.demodulize 
like image 161
Uelb Avatar answered Sep 17 '22 21:09

Uelb