Feature #10018

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

Added by Robert A. Heiler about 1 year ago. Updated about 1 year ago.

[ruby-core:63601]
Status:Feedback
Priority:Normal
Assignee:-

Description

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
this.

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

History

#1 Updated by Robert A. Heiler about 1 year 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 Updated by Nobuyoshi Nakada about 1 year 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?))
end

Wrapper method like Module#mix may help you.
http://www.slideshare.net/yukihiro_matz/rubyconf-2010-keynote-by-matz

It has been removed finally, you can construct

#3 Updated by Yukihiro Matsumoto about 1 year 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.

Matz.

Also available in: Atom PDF