How to get only class name without namespace How to get only class name without namespace ruby ruby

How to get only class name 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


The canonical way to do this is to invoke Object#class and Module#name. For example:

bar.class.name.split('::').last#=> "Bar"


I believe this would work fine too:

module Foo  class Bar  endendbar = Foo::Bar.newprint bar.class.to_s.split('::').last

This would result in

Bar

I also believe it would be a bit faster than the regular expression evaluation, but I'm not sure about this and I haven't performed a benchmark.