ruby only include module 1 time (prevent including a module multiple times )

访问量: 1397

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]

订阅/RSS Feed

Subscribe

分类/category