Module: Tins::ModuleGroup

Defined in:
lib/tins/module_group.rb

Overview

A module that allows grouping multiple modules together for type checking.

This implementation creates a shared module that gets included in each of the specified modules, enabling the use of ‘===` for type checking across all grouped modules simultaneously.

Class Method Summary collapse

Class Method Details

.[](*modules) ⇒ Module

Note:

This modifies the included modules by adding the new module to their inheritance chain, which can have side effects in complex class hierarchies.

Creates a new module group from the given modules.

This method dynamically creates an anonymous module and includes it in each of the provided modules. The returned module can then be used with the ‘===` operator for type checking across all grouped modules.

Examples:

Creating a module group

MyGroup = Tins::ModuleGroup[Array, String, Hash]
MyGroup === []     # => true
MyGroup === ""     # => true
MyGroup === {}     # => true

case some_value
when MyGroup
  handle_collection_or_string(some_value)
end

Parameters:

  • modules (Array<Module>)

    One or more modules to include in this group

Returns:

  • (Module)

    A new anonymous module that is included in all passed modules



30
31
32
33
34
35
36
# File 'lib/tins/module_group.rb', line 30

def self.[](*modules)
  modul = Module.new
  modules.each do |m|
    m.module_eval { include modul }
  end
  modul
end