Class: Module

Inherits:
Object show all
Defined in:
lib/jinx/helpers/module.rb,
lib/jinx/helpers/pretty_print.rb

Instance Method Summary collapse

Instance Method Details

#module_with_name(name) ⇒ Module?

Returns the class or module with the given name defined in this module. The name can qualified by parent modules, e.g. MyApp::Person. If name cannot be resolved as a Module, then this method returns nil.

Parameters:

Returns:

  • (Module, nil)

    the class or module defined in this module, or nil if none



8
9
10
# File 'lib/jinx/helpers/module.rb', line 8

def module_with_name(name)
  name.split('::').inject(self) { |parent, part| parent.const_get(part) } rescue nil
end

#parent_moduleModule

Returns this module’s definition context.

Examples:

A::B.parent_module #=> A

Returns:

  • (Module)

    this module’s definition context



15
16
17
# File 'lib/jinx/helpers/module.rb', line 15

def parent_module
  Kernel.module_with_name(name.split('::')[0..-2].join('::'))
end

#qpString

Returns the demodulized name.

Returns:

  • (String)

    the demodulized name



106
107
108
# File 'lib/jinx/helpers/pretty_print.rb', line 106

def qp
  name[/\w+$/]
end