mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Demodulize A Class Name as a rails til
This commit is contained in:
@@ -249,6 +249,7 @@ _390 TILs and counting..._
|
||||
- [Conditional Class Selectors in Haml](rails/conditional-class-selectors-in-haml.md)
|
||||
- [Creating Records of Has_One Associations](rails/creating-records-of-has-one-associations.md)
|
||||
- [Custom Validation Message](rails/custom-validation-message.md)
|
||||
- [Demodulize A Class Name](rails/demodulize-a-class-name.md)
|
||||
- [Hash Slicing](rails/hash-slicing.md)
|
||||
- [Ignore Poltergeist JavaScript Errors](rails/ignore-poltergeist-javascript-errors.md)
|
||||
- [Migrating Up Down Up](rails/migrating-up-down-up.md)
|
||||
|
||||
28
rails/demodulize-a-class-name.md
Normal file
28
rails/demodulize-a-class-name.md
Normal 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"
|
||||
```
|
||||
Reference in New Issue
Block a user