Project

General

Profile

Actions

Feature #17380

closed

Useful `include/prepend` in `refine`

Added by marcandre (Marc-Andre Lafortune) almost 4 years ago. Updated about 3 years ago.

Status:
Closed
Target version:
-
[ruby-core:101326]

Description

Currently, prepend/include within a refine block leads to a method not being to see itself, or others defined in the same module:

module Code
  def recurse(value = nil)
    return value if value

    recurse(42) # => NoMethodError!!!
  end
end

module Extension
  refine Object do
    include Code
  end
end

using Extension
:x.recurse(:y) # => :y (ok)
:x.recurse     # => NoMethodError, was hoping for 42

I find this unintuitive and not useful.

The conclusion of the current situation from @shugo (Shugo Maeda) and others is "I don't recommend module inclusion to define refined methods"

Could we change this situation so it can be recommended to use it?

What I believe would be more useful and is what I expected was that include/prepend within a Module would bring in the current methods in the Module, with the current refinements activated.

One use-case in particular is to publish libraries where one can give the option to the user to either:

  • call using GreatExtension in each and every file that need it
  • or MyClass.prepend GreatExtension once.

While Jeremy Evans found a way to do it, it remains challenging and unnatural.


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #17374: Refined methods aren't visible from a refinement's moduleRejectedshugo (Shugo Maeda)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0