Home Rubyonlyincludemodule1time(preventincludingamodulemultipletimes)
Post
Cancel

Rubyonlyincludemodule1time(preventincludingamodulemultipletimes)

approaches:

1. use raw ruby ( not recommended)   

2. use ActiveSupport::Concern ( http://api.rubyonrails.org/classes/ActiveSupport/Concern.html )

p.s. 3. require_dependency  ( this is used to require a file ) 

p.s.2 实际上多次包含一个module 也不会出太大的错,除了 会多次调用 included 方法。如下:

module Tree
    def name;         puts "I am a tree~" l;     end 
end
class Apple
    include Tree
    include Tree
    include Tree
    include Tree
end
puts Apple.included_modules #=> [Tree, Kernel]
This post is licensed under CC BY 4.0 by the author.