gpt4 book ai didi

ruby-on-rails - 在文件中的特定行插入行

转载 作者:太空宇宙 更新时间:2023-11-03 17:19:07 26 4
gpt4 key购买 nike

我已经在 r+ 模式下打开了我现有的文件。

打开(“#{RAILS_ROOT}/locale/app.pot”,'r+')做|f|

结束

我想在特定行号插入一些其他行..就像我想在第 10 行插入“Hii”。第 2 行的“你好”。第 20 行的“world”。

我如何在 ruby​​ 中处理它??

最佳答案

这在过去对我有用:

def write_at(fname, at_line, sdat)
open(fname, 'r+') do |f|
while (at_line-=1) > 0 # read up to the line you want to write after
f.readline
end
pos = f.pos # save your position in the file
rest = f.read # save the rest of the file
f.seek pos # go back to the old position
f.write sdat # write new data
f.write rest # write rest of file
end
end

关于ruby-on-rails - 在文件中的特定行插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3320220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com