Why does this code only modify the last file in the array? The files are both readable and writeable by my user.
%w(views/layout.rhtml views/admin/layout.rhtml).each do |file|
text = File.read(file)
File.open(file, 'w+') do |f|
f << text.gsub(/\?v=(\d+)/, "?v=#{$1.to_i + 1}")
end
end
Your problem is that you’re not using the block form of
gsubso$1and similar globals are not set as you think they are. From the fine manual:And:
Bolding mine. Also, I left out the backtick global:
To avoid problems with the Markdown (if anyone knows how to get a backtick in an inlined code block I’d appreciate a pointer).
If you do this instead:
I think you’ll get the results that you’re looking for.