Jay L. wrote:
On Mon, 8 Oct 2007 01:19:01 +0900, Sebastian H. wrote:
The code posted above gives you permission errors? Or your original code gives
you permission errors? Because the code posted above really shouldn’t. If the
code above gives you permission error, it’s not because of open file handles.
Actually, the code above should give him
TypeError: can’t convert String into Integer
because IO.open wants an integer file descriptor He wants
Kernel.open.
The working code is:
Dir.glob("*").each do |file|
if /%!PS-Adobe/ =~ open(file) {|f| f.gets}
File.rename(file, file + “.eps”)
end
end
Peter, the key to Nobuyoshi’s change above is that the rename now
happens
outside the Kernel.open block. It doesn’t matter that it happens
inside
the if/end block; that’s not what’s holding the file open - Kernel.open
is.
Now, the only thing that’s done in the block is the gets, and the block
is
closed on the very same line, before the rename happens.
Yes, Jay, that worked! And, I beg your pardon, Nobuyoshi, because, I
rather glibly just glanced over your code thinking you were just
repeating my code.
Now, I have to admit, I really need to look at your code, Jay, because,
to me, it’s like a different “dialect.” My primitive Ruby experience is
showing, I guess. I’ve certainly worked with blocks before, and I’ve
certainly worked with opening files before. And, I thought I was being
clever here by using “gets” for the first time, knowing that I only
wanted to read that first line. Previously, I’ve used File.read a lot,
but, I actually did want to read in the whole file then. But, your
casual use of a black and opening a file here kind of blows me away,
actually.
Thank you everyone.
Cheers,
Peter