txt = open("keywords.txt") filenames = ["actions.txt", "directions.txt", "movement.txt"] dictionary = {} txt.each_line do |line| filenames.each do |name| if File.read(name).include?line dictionary[name.chomp('.txt')] = line.chomp("\n") end end end puts dictionary