Ruby 检查数组是否包含字符串,不区分大小写

Ruby 检查数组是否包含字符串,不区分大小写,ruby,arrays,string,Ruby,Arrays,String,我正在尝试编写一个从单词列表中删除单词的应用程序: puts "Words:" text = gets.chomp puts "Words to remove:" remove = gets.chomp words = text.split(" ") removes = remove.split(" ") words.each do |x| if removes.include.upcase? x.upcase print "REMOVED " else

我正在尝试编写一个从单词列表中删除单词的应用程序:

puts "Words:"
text = gets.chomp
puts "Words to remove:"
remove = gets.chomp
words = text.split(" ")
removes = remove.split(" ")
words.each do |x| 
    if removes.include.upcase? x.upcase
        print "REMOVED "
    else
        print x, " "
    end
end
我如何使它不区分大小写? 我试着把箱子放进去,但运气不好

puts "Words:"
text = gets.chomp
puts "Words to remove:"
remove = gets.chomp
words = text.split(" ")
removes = remove.upcase.split(" ")

words.each do |x|
  if removes.include? x.upcase
    print "REMOVED "
  else
    print x, " "
  end
end
如果块生成true,arrayselect将从数组中选择任何元素。因此,如果select不选择任何元素并返回空数组,则该元素不在数组中

编辑

还可以使用if removes.index{| i | i.downcase==x.downcase}。它的性能优于select,因为它不创建临时数组,并且在找到第一个匹配项时返回

如果块生成true,arrayselect将从数组中选择任何元素。因此,如果select不选择任何元素并返回空数组,则该元素不在数组中

编辑


还可以使用if removes.index{| i | i.downcase==x.downcase}。它的性能优于select,因为它不创建临时数组,并且在找到第一个匹配项时返回。

在何处?不清楚你在if声明中尝试了什么。编辑了OPA,你不需要对每个元素都加上大小写吗?在哪里?不清楚你在if声明中尝试了什么。编辑了OPA,你不需要对每个元素都加上大小写吗?不是我期望的那样,但它很有效。我更喜欢将移除物保留在原始外壳中。然后将其保持在正常情况下,而使用:removes.any?{| r | r.upcase==x.upcase}这并不是我所期望的,但它确实有效。我更喜欢将移除物保留在原始外壳中。然后将其保持在正常情况下,而使用:removes.any?{| r | r.upcase==x.upcase}
words.each do |x| 
    if removes.select{|i| i.downcase == x.downcase} != []
        print "REMOVED "
    else
        print x, " "
    end
end