Created
April 20, 2012 07:23
-
-
Save kinopyo/2426829 to your computer and use it in GitHub Desktop.
Ruby: Get module name or class name without module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
n = Notification::Answer.new | |
n.class.name.split('::').last || '' | |
# => "Answer" | |
n.class.name.split('::').first || '' | |
# => "Notification" |
An update on the comment from @phstc that works for nested modules:
n = Foo::Bar::Bazz.new
n.class.to_s.gsub(/^.*::/, '')
# => "Bazz"
n.to_s.gsub(/(.*)::.*/, '\1')
# => "Foo::Bar"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
class_name