Skip to content

Instantly share code, notes, and snippets.

@panozzaj
Created May 14, 2010 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save panozzaj/401660 to your computer and use it in GitHub Desktop.
Save panozzaj/401660 to your computer and use it in GitHub Desktop.
Works with Ruby 1.8.7, but not Ruby 1.9. This could have been a problem because I manually installed Ruby 1.9.
I would guess this is a bug in fileutils, a Windows-related problem, or some combination thereof.
#####
# test.rb
require 'fileutils'
filenames = ARGV
filenames.each do |filename|
newfile = File.open("#{filename}new", "w")
File.open(filename).each do |line|
newfile.print line
end
newfile.close
FileUtils.mv("#{filename}new", filename, :verbose => true)
end
#####
> ruby test.rb file.rb
mv file.rbnew file.rb
> ruby19 test.rb file.rb
mv file.rbnew file.rb
C:/cygwin/lib/ruby/1.9.1/fileutils.rb:1298:in `unlink': Permission denied - file.rb (Errno::EACCES)
from C:/cygwin/lib/ruby/1.9.1/fileutils.rb:1298:in `block in remove_file'
from C:/cygwin/lib/ruby/1.9.1/fileutils.rb:1306:in `platform_support'
from C:/cygwin/lib/ruby/1.9.1/fileutils.rb:1297:in `remove_file'
from C:/cygwin/lib/ruby/1.9.1/fileutils.rb:509:in `block in mv'
from C:/cygwin/lib/ruby/1.9.1/fileutils.rb:1396:in `block in fu_each_src_dest'
from C:/cygwin/lib/ruby/1.9.1/fileutils.rb:1412:in `fu_each_src_dest0'
from C:/cygwin/lib/ruby/1.9.1/fileutils.rb:1394:in `fu_each_src_dest'
from C:/cygwin/lib/ruby/1.9.1/fileutils.rb:502:in `mv'
from test.rb:11:in `block in <main>'
from test.rb:5:in `each'
from test.rb:5:in `<main>'
#####
Workaround:
require 'fileutils'
filenames = ARGV
filenames.each do |filename|
newfile = File.open("#{filename}new", "w")
file = File.open(filename, "r")
while (line = file.gets)
newfile.print line
end
file.close
newfile.close
FileUtils.mv("#{filename}new", filename, :verbose => true)
end
#####
^^^^^ Works as expected for Ruby 1.8 and Ruby 1.9, but is more verbose.
Again, I would guess this is a bug in fileutils, a Windows-related problem, or some combination thereof.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment