Ruby: module, require and include

前端 未结 4 1973
感动是毒
感动是毒 2020-12-23 17:39

I\'m trying to use Ruby modules (mixins).

I have test.rb:

#!/usr/bin/env ruby
require_relative \'lib/mymodule\'

class MyApp
  include MyModule
  sel         


        
4条回答
  •  一生所求
    2020-12-23 17:59

    In short: you need to extend instead of include the module.

    class MyApp
      extend MyModule
      self.hallo
    end
    

    include provides instance methods for the class that mixes it in.

    extend provides class methods for the class that mixes it in.

    Give this a read.

提交回复
热议问题