HTMLソースコードから8文字以上のすべての単語を取得する基本的なルビースクレーパーを作成しようとしています。次に、単語の最初の文字に対応するファイルにこれらを保存します。シンプルですね。
re = /\w{8,}/
cre = /[a-z0-9]/
a = b.html #This grabs the html from the browser
matchx = a.scan(re)
matchx.each do |xx|
word = xx.to_s.downcase.chomp
fchar = word[0].chr
if (fchar.match(cre)) #Not sure if I need this
@pcount += 1
fname = @WordsFName+fchar #@WordsFName is a prefix
tmpF = File.open(fname,"a+")
#Check for duplicates, if not write to file
exists = File.readlines(fname).any? { |li| li[word] }
if (!exists)
tmpF.write(word+"\n")
print word
@wcount += 1
end
end
end
Ruby はすべての単語を正常に取得し、最初の文字を取得し、必要なすべてのファイルを開きますが、書き込みに失敗します。また、 print メソッドは重複を含むすべての単語を出力しますが、いずれかを検査しますか? irbの方法は問題ありませんでした..