我需要从数组中删除任何不是特定长度的字符串。作业建议使用map
or map!
。我一直在玩map!
,delete_if
和keep_if
, 但我无处可去。谁能帮我?以下是我的尝试之一:
dictionary = Array.new
file = File.open("words.txt").each do | line |
dictionary.push line
end
(wordlength = gets)
wordlength = wordlength.to_i
dictionary = dictionary.reject {|word| word.length != wordlength }
puts dictionary