Ruby: module, require and include

前端 未结 4 1952
感动是毒
感动是毒 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:49

    Your code is working - but including a module does not do what you think it does. The class including the module will not get the methods - the objects from this class will.

    So this will work :

    class MyApp
      include MyModule
    end
    
    my_app_object = MyApp.new
    my_app_object.hallo # => hallo
    

    my_app_object is an object of class MyApp, which has a mixins of module MyModule. Take a look there for a complete explanation of modules and mixins.

提交回复
热议问题