Method: Module.basename

Defined in:
lib/quality_extensions/module/basename.rb

.basename(module_or_name) ⇒ Object

Gets the basename of a “module path” (the name of the module without any of the namespace modules that it is contained in), in the same sense that File.basename returns the basename of a filesystem path.

This is identical to Facets’ String#basename (‘facets/string/basename’) except that:

  • it is a class method instead of an instance method of String,

  • it accepts modules, strings, and symbols.

See also Module.dirname/Module.namespace_name_of.

These can be used together, such that the following is always true:

OuterModule::MiddleModule::InnerModule == Module.join(Module.dirname(some_module), Module.basename(some_module)).constantize


27
28
29
30
31
32
33
34
35
36
# File 'lib/quality_extensions/module/basename.rb', line 27

def self.basename(module_or_name)
  case module_or_name
    when Module
      module_or_name.basename
    when Symbol,String
      module_or_name.to_s.gsub(/^.*::/, '')
    else
      raise ArgumentError
  end
end