我想将一行与一个文本文件给出的另一行连接起来。我怎么做?
我已经尝试使用带索引的数组,但每当我执行line + line[index + 1]
时,它都不会将前一行与第二行连接起来。
def createWordList(filename)
wordArray = Array.new
for i in 1..6
i = gets.chomp
i.delete("\n\r\t")
wordArray.push(i)
end
file = File.open(filename, "r+")
wordArray.each_with_index do |item, index|
file.puts(item)
item += item[index + 1]
file.puts(item)
end
end
createWordList("words.txt")
Ì期待line[index + 1]
返回下一行,但它返回了line[index + 1]
的信
虽然我无法理解为什么以及你想要实现什么,但我可以弄清楚,基本上你正在寻找/调用行作为索引。
请参考this。
=> file = IO.readlines('filename') # => ["line 1\n", "line 2\n", "line 3\n"]
=> file[2] # => "line 3\n"