rubyのnamespacesについて

850 ワード

rubyのnamespacesに対していくつか理解して分かち合います
 
主に使われるnamespacesのキーワードはmodule,require,load,includeです
 
moduleの役割は、ネーミングスペースを絞って理解するのは簡単ですが、require、load、includeの理解については、まず次のコードを見てみましょう.
 
 
#A.rb
module All
def self.a
	print "a
" end end print "loading in the A.rb
" #B.rb module All def self.b print "b
" end end #test.rb require "A" require "B" require "A" require "A" load "A.rb" load "A.rb" All.a All.b 

結果は次のとおりです.
loading in the A.rb loading in the A.rb
loading in the A.rb a
b
requireはロードされたファイルに対して1回しかロードされていないので、1つ目はloading in the A.rbです.requireが機能した後、まだ多くのrequireがありますが、印刷をトリガしていません.loading in the A.rb、2行目はload「A.rb」がトリガし、出現するたびにロードされたファイルが新しくロードされます.includeはmixinです