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

前端 未结 10 1316
情歌与酒
情歌与酒 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:56

    Another way to do it if you "own" the module is to use module_function.

    module UsefulThings
      def a
        puts "aaay"
      end
      module_function :a
    
      def b
        puts "beee"
      end
    end
    
    def test
      UsefulThings.a
      UsefulThings.b # Fails!  Not a module method
    end
    
    test
    

提交回复
热议问题