2017-11-11 8 views
0

私はそれが間違っていると確信しています。しかし、私はこのプログラムが "phone_numbers.txt"を実行し、繰り返し以外のものを置き換えることを望みます。["Employee Marked Urgency as: low", "Employee Marked Urgency as: high"]このコードの結果はまったくありません。何も変わっていません。任意のヒント?いくつかのフレーズテキストファイル以外のルビーを検索して置き換えます

require 'digest/sha1' # to generate uniqs 

file_names = ['phone_numbers.txt'] 
words_to_exclude = ["Employee Marked Urgency as: low", 
        "Employee Marked Urgency as: high"] 

file_names.each do |file_name| 
    res = File.readlines(file_name).each_with_index.uniq do |s, idx| 
    words_to_exclude.include?(s) ? 
     Digest::SHA1.hexdigest(idx.to_s) : s 
    end.join("\n") 
    File.write(file_name, res) 
end 

はここで何が起こっている:私たちは私たちのリストから単語に遭遇した場合、我々は生成

file_names = ['phone_numbers.txt'] 
    file_names.each do |file_name| 
     words_to_exclude = ["Employee Marked Urgency as: low", "Employee Marked Urgency as: high"] 
     text = File.read(file_name) 
     lines = text.split("\n") 
     new_contents = lines.uniq.reject do |word| 
     words_to_exclude.include? word 
     end.join("\n") 
     File.open(file_name, "w") { |file| file.puts new_contents } 
    end 
+1

コードを確認するだけの人が必要な場合は、[コードレビュー](https://codereview.stackexchange.com/)で質問する必要があります。あなたのコードが何をしたいのかというような疑問がある場合や、実行する際にエラーが発生した場合は、その特定の質問に詳細を追加してください。 – Mehraban

答えて

0

私はトリックを行う必要がありますブロックでArray#uniqを使用してそれを正しく理解したが、わかりませんとにかくuniq十分な文字列を渡すことができます。それ以外の場合は元の文字列を使用して比較します。

+0

あなたのコードに間違いがあると思います。私はそれを実行しようとしましたが、何もエラーを返しません。 – Yeager

+0

''ブロックin

':未定義メソッド 'with_index' for#(NoMethodError)' – Yeager

+0

ファイルが手元にないのでテストできませんでしたしかし、私はあなたが良い出発点を持っていると信じています。 – mudasobwa

関連する問題