有一个单词列表和禁止单词列表.我想通过单词列表并编辑所有被禁止的单词.这就是我最终做的事情(请注意catched boolean):
puts "Give input text:" text = gets.chomp puts "Give redacted word:" redacted = gets.chomp words = text.split(" ") redacted = redacted.split(" ") catched = false words.each do |word| redacted.each do |redacted_word| if word == redacted_word catched = true print "REDACTED " break end end if catched == true catched = false else print word + " " end end
有没有适当/有效的方法?