我很確定我錯了。但我希望這個程序能夠通過「phone_numbers.txt」運行,並取代除["Employee Marked Urgency as: low", "Employee Marked Urgency as: high"]
之外的任何重複內容。此代碼的結果完全不是。沒有改變。有小費嗎?查找並替換紅寶石,除了一些短語文本文件
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
如果您只需要某人查看您的代碼,則應該在[代碼評論](https://codereview.stackexchange.com/)上提出您的問題。如果您有任何問題,例如您的代碼不符合您的要求,或者在運行時出現錯誤,請添加具體問題並提供詳細信息。 – Mehraban