2017-11-11 10 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

': # (NoMethodError)에 대해 정의되지 않은 메소드'with_index'' – Yeager

+0

파일이 없기 때문에 이것을 테스트 할 수 없었다. ,하지만 당신이 좋은 출발점을 가지고 있다고 생각합니다. – mudasobwa