19

I have a module of following

module SimpleTask
    def task1
    end
    def task2
    end
    def task3
    end
end

And I have a model which requires only task2 method of module SimpleTask.

I know including SimpleTask in my model with include SimpleTask would do the job.

But I wonder if I can only include specific task2 method in my model.

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
Gagan
  • 4,278
  • 7
  • 46
  • 71
  • possible duplicate of [Can I invoke an instance method on a Ruby module without including it?](http://stackoverflow.com/questions/322470/can-i-invoke-an-instance-method-on-a-ruby-module-without-including-it) – Ciro Santilli OurBigBook.com Nov 01 '14 at 09:42

4 Answers4

8

It sounds like you need to refactor #task2 into a separate module (e.g., BaseTask). Then you can easily include only BaseTask where you only need #task2.

module BaseTask
  def task2
    ...
  end
end

module SimpleTask
  include BaseTask

  def task1
    ...
  end

  def task3
    ...
  end
end

It's hard to help much more without a more concrete question (such as interdependence between the methods of SimpleTask, etc.

You could do some meta-programming where you include SimpleTask and then undefine the methods you don't want, but that's pretty ugly IMO.

krohrbaugh
  • 1,324
  • 9
  • 11
  • 2
    There's nothing wrong with the question, it's a good one and is very clear. I got to this page because I have the exact same question. I don't want to create a new module, nor do I want to include the other methods in the class namespace unnecessarily. I just want to include a single method from the module. – Keith Bennett Dec 25 '20 at 04:45
6

I'm going to steal an example from delegate.rb, it restricts what it includes

...
class Delegator < BasicObject
  kernel = ::Kernel.dup
  kernel.class_eval do
    [:to_s,:inspect,:=~,:!~,:===,:<=>,:eql?,:hash].each do |m|
      undef_method m
    end
  end
  include kernel
...

becomes

module PreciseInclude

  def include_except(mod, *except)
    the_module = mod.dup
    the_module.class_eval do
      except.each do |m|
        remove_method m # was undef_method, that prevents parent calls
      end
    end
    include the_module
  end
end

class Foo
  extend PreciseInclude

  include_except(SimpleTask, :task1, :task2)
end

Foo.instance_methods.grep(/task/) => [:task3]

you can always flip it so instead of include it becomes include_only

The catch is that remove_method won't work for nested modules, and using undef will prevent searching the entire hierarchy for that method.

jtzero
  • 2,204
  • 2
  • 25
  • 44
6

You could add

module SimpleTask
    def task1
    end
    def task2
    end
    def task3
    end
    module_function :task2
end

So that you can call the method like a class method on the module as well as having it as an instance method in the places you do want all three methods, ie:

class Foo
   include SimpleTask
end #=> Foo.new.task2
class LessFoo
   def only_needs_the_one_method
      SimpleTask.task2
   end
end #=> LessFoo.new.only_needs_the_one_method

Or, if there's really no shared state in the module and you don't mind always using the module name itself, you can just declare all the methods class-level like so:

module SimpleTask
    def self.task1
    end
    def self.task2
    end
    def self.task3
    end
end

class Foo
   include SimpleTask # Does, more or less nothing now
   def do_something
     SimpleTask.task1
   end
end 
#=> Foo.new.task2 #=> "task2 not a method or variable in Foo"
#=> Foo.new.do_something does, however, work
class LessFoo
   def only_needs_the_one_method
      SimpleTask.task2
   end
end #=> LessFoo.new.only_needs_the_one_method works as well in this case

But you'd have to change all the callers in that case.

Tim Snowhite
  • 3,736
  • 2
  • 23
  • 27
  • `module_function` is great as long as it's ok that all of the module instance methods are private. If you want them to be public you can use `extend self`. One has to be careful that none of the methods rely on state of the instance which includes them; in that case a class method would not work. – Keith Bennett Dec 25 '20 at 04:58
3

A simple solution for this is

define_method :task2, SimpleTask.instance_method(:task2)

Matthias Michael Engh
  • 1,159
  • 2
  • 10
  • 25