【发布时间】:2017-11-11 14:39:31
【问题描述】:
我很确定我弄错了。但我希望这个程序运行完“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
【问题讨论】:
-
如果您只需要有人检查您的代码,您应该在code review 上提问。如果您有任何问题,例如为什么您的代码不符合您的要求,或者您在运行时遇到错误,请添加该特定问题的详细信息。
标签: json ruby replace find repeat