Project

General

Profile

Actions

Feature #11927

open

Return value for `Module#include` and `Module#prepend`

Added by sawa (Tsuyoshi Sawada) almost 9 years ago. Updated about 4 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:72617]

Description

Currently, Module#include and Module#prepend return the receiver, regardless of whether the ancestor chain has been modified. It is not straightforward to know whether it actually had effect.

module A; end
module B; end
A.include B # => A
A.ancestors # => [A, B]
A.prepend B # => A
A.ancestors # => [A, B]

I propose that, when Module#include and Module#prepend have no effect, they should either:

(1) return nil
(2) return false, or
(3) raise an exception

This is similar to Kernel#require, which returns false when it has no effect. To make it parallel with Kernel#require, it might be even better to return true when Module#include and Module#prepend have effect, and false otherwise. It makes no sense to return the receiver because that is known.

Some relevant cases with expectations are:

  • prepend after include
module A; end
module B; end
A.include B # => A/true
A.prepend B # => nil/false/exception
  • include after prepend
module A; end
module B; end
A.prepend B # => A/true
A.include B # => nil/false/exception
  • include/prepend after include/include at superclass
class A; end
module B; end
A.include M # => A/true
class B < A; end
B.include M # => nil/false/exception

Updated by fatkodima (Dima Fatko) about 4 years ago

I would like to have this. Please, reconsider this feature.

Actions #2

Updated by sawa (Tsuyoshi Sawada) about 4 years ago

  • Description updated (diff)
Actions #3

Updated by sawa (Tsuyoshi Sawada) about 4 years ago

  • Description updated (diff)

Updated by marcandre (Marc-Andre Lafortune) about 4 years ago

It would help to:

  1. have an example of use case

  2. discuss why B.include M unless B < M is not equivalent / sufficient

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0