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?
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
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