Ruby include module's single method in model

前端 未结 4 2298
难免孤独
难免孤独 2021-02-19 00:28

I have a module of following

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

And I have a model which r

4条回答
  •  故里飘歌
    2021-02-19 01:18

    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.

提交回复
热议问题