【发布时间】:2015-03-18 18:44:32
【问题描述】:
尝试像Replace content in a file between two markers 那样做,但接受的答案似乎不起作用:
index.html
<!--start-->Hello world<!--end-->
myscript.rb
def replace(file_path, contents)
file = File.open(file_path, "r+")
html = ""
while(!file.eof?)
html += file.readline
end
file.close()
return html.gsub(/<!--start-->(.*)<!--end-->/im, contents)
end
thing = ["Foo", "Bar", "Baz"].sample
replace("/path/to/index.html", thing)
运行ruby myscript.rb 后,index.html 保持不变。我在 Ruby 2.2.0 上。
【问题讨论】:
标签: ruby