1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Demodulize A Class Name as a rails til

This commit is contained in:
jbranchaud
2016-04-11 16:39:38 -05:00
parent 55687af878
commit 4be459cd02
2 changed files with 29 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
# Demodulize A Class Name
If you call `.class.name` on an instance of some class, the fully qualified
name will be returned, module names and all. Consider the following example
class:
```ruby
module One
module Two
class Three
...
end
end
end
```
```ruby
> One::Two::Three.new.class.name
#=> "One::Two::Three"
```
If you just want the unqualified class name; modules not included, you can
use the `#demodulize` method provided by `ActiveSupport`.
```ruby
> One::Two::Three.new.class.name.demodulize
#=> "Three"
```