Feature #10018

Consider adding Sub-Includes as in include Foo::bar

Added by shevegen (Robert A. Heiler) over 6 years ago. Updated over 6 years ago.

Target version:


On IRC someone asked a nice question:

<arup_r> If I write class Foo; include Enumerable; end This code include all methods from Enumerable. But suppose I want to include #all?, #any? .. What's the way to include only required methods ?

This prompted me to write this.

What if ruby would allow us to include just singular methods?

Something like:

include Enumerable::any?

And so forth, so that we could cherry-pick what we want.

This is already possible in some projects, like facets.

But I think they solve it by splitting up the whole
stuff into individual files, and then including

I think it would be nicer if include itself would
support a scope-mechanic.

Updated by shevegen (Robert A. Heiler) over 6 years ago

Oops sorry!

This is not a bug, it is a feature request.

I am not sure how to move it to features now, sorry. :(

Updated by nobu (Nobuyoshi Nakada) over 6 years ago

  • Tracker changed from Bug to Feature
  • Description updated (diff)
  • Status changed from Open to Feedback

You can achieve it by "method transplanting".

class Foo
  define_method(:any?, Enumerable.instance_method(:any?))

Wrapper method like Module#mix may help you.

It has been removed finally, you can construct

Updated by matz (Yukihiro Matsumoto) over 6 years ago

And method transplanting only works when the method does not call super nor other Enumerable methods.
Automatic resolving is possible, but it would make inclusion far more complex.


Also available in: Atom PDF