小编Mik*_*dru的帖子

删除另一个数组中存在的数组元素

有一个单词列表和禁止单词列表.我想通过单词列表并编辑所有被禁止的单词.这就是我最终做的事情(请注意catched布尔值):

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
Run Code Online (Sandbox Code Playgroud)

有没有适当/有效的方法?

ruby arrays array-difference

9
推荐指数
2
解决办法
2574
查看次数

标签 统计

array-difference ×1

arrays ×1

ruby ×1