Method: Kernel#remove_const!
- Defined in:
- lib/qualitysmith_extensions/module/remove_const.rb
#remove_const! ⇒ Object
This is similar to Kernel#remove_const, but it only works for modules/classes.
This is similar to the built-in Module#remove_module, but it lets you do it in a more object oriented manner, calling remove! on the module/class/constant itself that you want to remove, rather than on its parent.
Makes it possible to write simply:
A::B::C.remove_const!
rather than having to think about which module the constant is actually defined in and calling remove_const on that module. This is how you would have to otherwise do it:
A::B.send(:remove_const, :C)
27 28 29 30 31 32 33 34 35 |
# File 'lib/qualitysmith_extensions/module/remove_const.rb', line 27 def remove_const! if split.size > 1 parent_module = modspace # For example, would be A::B for A::B::C const_to_remove = split.last # For example, would be :C for A::B::C parent_module.ignore_access.remove_const(const_to_remove) else Object.ignore_access.remove_const(name) end end |