Feature #10018

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

Added by Robert A. Heiler over 2 years ago. Updated over 2 years ago.



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.


#1 [ruby-core:63602] Updated by Robert A. Heiler over 2 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. :(

#2 [ruby-core:63603] Updated by Nobuyoshi Nakada over 2 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

#3 [ruby-core:63618] Updated by Yukihiro Matsumoto over 2 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