1

単語のリストから単語を削除するアプリケーションを作成しようとしています:

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

この大文字と小文字を区別しないようにするにはどうすればよいですか? そこに入れてみ.upcaseましたが、うまくいきませんでした。

4

2 に答える 2

3
words.each do |x| 
    if removes.select{|i| i.downcase == x.downcase} != []
        print "REMOVED "
    else
        print x, " "
    end
end

array#selectブロックが true を返す場合、配列から任意の要素を選択します。したがって、select要素を選択せず​​に空の配列を返す場合、それは配列に含まれていません。


編集

も使用できますif removes.index{|i| i.downcase==x.downcase}select一時的な配列を作成せず、最初の一致が見つかるたびに戻るため、パフォーマンスは向上します。

于 2013-02-09T23:03:59.367 に答える
2
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
于 2013-02-09T22:56:20.787 に答える