コードの不具合を解決した時のメモ書き。
rubyでgem 'hashie'を利用してyamlをロードした際に警告が出て、処理も期待通りにならなかった。
code.rb
@sites = Hashie::Mash.new YAML.load_file File.join(ROOT_PATH, './target_site.yml')
puts @sites.neko.first.index
target_site.yml
neko:
-
url: 'https://catfood/product/1.html'
selector: 'div.top_box .zaiko'
index: 0
実行時のwarnning
WARN -- : You are setting a key that conflicts with a built-in method Hashie::Mash#index defined in Hash. This can cause unexpected behavior when accessing the key via as a property. You can still access the key via the #[] method.
これは警告文から分かる通りyamlの項目名に index が含まれていることが原因。
Hashie::Mashのインスタンスが元々indexメソッドを持っているため、これをyamlで項目名として使うとメソッド名が衝突するため警告が出る。
実際の処理では、@sites.neko.first.indexを実行しようとすると元々Hashie::Mashが持つindexメソッドが呼ばれ引数不足でエラーが発生する。また、警告文の末尾にもある通り、以下のような呼び出し方によってyamlのindex項目を呼び出すことは出来る。
@sites.neko.first[:index]
実際のコーディングではyamlでindexという項目名をindex_numと差し替えて実装を進めた。
現場から以上です。╭( ・ㅂ・)و