Sha256: b36634561575706f93fda8c55c20f717081ba24728566f40a39f0106bec1f1e2

Contents?: true

Size: 1.07 KB

Versions: 70

Compression:

Stored size: 1.07 KB

Contents

# Module Initialize

## Introduction

A module is usually a mixin, so when an `#initialize` method is present it is
hard to tell initialization order and parameters so having `#initialize`
in a module is usually a bad idea.

## Example

The `Foo` module below contains a method `initialize`. Although class `B` inherits from `A`, the inclusion of `Foo` stops `A#initialize` from being called.

```Ruby
class A
  def initialize(a)
    @a = a
  end
end

module Foo
  def initialize(foo)
    @foo = foo
  end
end

class B < A
  include Foo

  def initialize(b)
    super('bar')
    @b = b
  end
end
```

A simple solution is to rename `Foo#initialize` and call that method by name:

```Ruby
module Foo
  def setup_foo_module(foo)
    @foo = foo
  end
end

class B < A
  include Foo

  def initialize(b)
    super 'bar'
    setup_foo_module('foo')
    @b = b
  end
end
```

## Current Support in Reek

Reek warns about module initialize when an instance method named `initialize` is present in a module.

## Configuration

Module Initialize supports the [Basic Smell Options](Basic-Smell-Options.md).

Version data entries

70 entries across 68 versions & 2 rubygems

Version Path
reek-6.0.3 docs/Module-Initialize.md
reek-6.0.2 docs/Module-Initialize.md
reek-6.0.1 docs/Module-Initialize.md
reek-6.0.0 docs/Module-Initialize.md
reek-5.6.0 docs/Module-Initialize.md
reek-5.5.0 docs/Module-Initialize.md
reek-5.4.1 docs/Module-Initialize.md
reek-5.4.0 docs/Module-Initialize.md
reek-5.3.2 docs/Module-Initialize.md
reek-5.3.1 docs/Module-Initialize.md
reek-5.3.0 docs/Module-Initialize.md
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/docs/Module-Initialize.md
config_gems_initialization_aim-0.1.4 vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/docs/Module-Initialize.md
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/config_gems_initialization_aim-0.1.1/vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/docs/Module-Initialize.md
config_gems_initialization_aim-0.1.3 vendor/bundle/ruby/2.5.0/gems/reek-5.2.0/docs/Module-Initialize.md
reek-5.2.0 docs/Module-Initialize.md
reek-5.1.0 docs/Module-Initialize.md
reek-5.0.2 docs/Module-Initialize.md
reek-5.0.1 docs/Module-Initialize.md
reek-5.0.0 docs/Module-Initialize.md