Can I invoke an instance method on a Ruby module without including it?

前端 未结 10 1327
情歌与酒
情歌与酒 2020-12-07 07:37

Background:

I have a module which declares a number of instance methods

module UsefulThings
  def get_file; ...
  def delete_file; ...

  def forma         


        
10条回答
  •  爱一瞬间的悲伤
    2020-12-07 07:54

    To invoke a module instance method without including the module (and without creating intermediary objects):

    class UsefulWorker
      def do_work
        UsefulThings.instance_method(:format_text).bind(self).call("abc")
        ...
      end
    end
    

提交回复
热议问题