# The old File.rename not working again

**URL:** <https://rubytalk.org/t/the-old-file-rename-not-working-again/41207>\
**Category:** ruby-talk\
**Created:** [7 October 2007 02:15 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207 "2007-10-07T02:15:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guest16](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Guest16](https://rubytalk.org/u/Guest16)\
**Post date:** [7 October 2007 02:15 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/1 "2007-10-07T02:15:04Z")

</div>

This always throws me. I keep getting "permission denied" messages when  
I simply try to rename files. Here's my code. The files in the directory  
start out without an extension.

Dir.chdir("F:/images")  
Dir.glob("\*").each do |file|  
&nbsp;&nbsp;File.new(file, "r").gets  
&nbsp;&nbsp;if $\_ =~ /%!PS-Adobe/  
&nbsp;&nbsp;&nbsp;&nbsp;newfile = file + ".eps"  
&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, newfile)  
&nbsp;&nbsp;end  
end

Thanks,  
Peter

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada1](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@Nobuyoshi\_Nakada1](https://rubytalk.org/u/Nobuyoshi_Nakada1)\
**Post date:** [7 October 2007 06:07 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/2 "2007-10-07T06:07:24Z")

</div>

Hi,

At Sun, 7 Oct 2007 11:15:04 +0900,  
Peter Bailey wrote in [ruby-talk:272952]:

> This always throws me. I keep getting "permission denied" messages when  
> I simply try to rename files. Here's my code. The files in the directory  
> start out without an extension.
> 
> Dir.chdir("F:/images")

Maybe on Windows?

> Dir.glob("\*").each do |file|  
> &nbsp;&nbsp;File.new(file, "r").gets

This leaves the file still opened, and on Windows, it's  
prohibited by the OS to move/rename files while they are  
opened.

&nbsp;&nbsp;&nbsp;&nbsp;if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file + ".eps")  
&nbsp;&nbsp;&nbsp;&nbsp;end

> **···**
>
> --  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [7 October 2007 13:22 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/3 "2007-10-07T13:22:44Z")

</div>

Why would he need to open the file at all?

Dir.chdir("F:/images")  
Dir.glob("\*").each do |file|  
&nbsp;&nbsp;if file =~ /%!PS-Adobe/  
&nbsp;&nbsp;&nbsp;&nbsp;newfile = file + ".eps"  
&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, newfile)  
&nbsp;&nbsp;end  
end

Regards,

Dan

> **···**
>
> On Oct 7, 12:07 am, Nobuyoshi Nakada \<n...@ruby-lang.org\> wrote:
> 
> > Hi,
> > 
> > At Sun, 7 Oct 2007 11:15:04 +0900,  
> > Peter Bailey wrote in [ruby-talk:272952]:
> > 
> > \> This always throws me. I keep getting "permission denied" messages when  
> > \> I simply try to rename files. Here's my code. The files in the directory  
> > \> start out without an extension.
> > 
> > \> Dir.chdir("F:/images")
> > 
> > Maybe on Windows?
> > 
> > \> Dir.glob("\*").each do |file|  
> > \> File.new(file, "r").gets
> > 
> > This leaves the file still opened, and on Windows, it's  
> > prohibited by the OS to move/rename files while they are  
> > opened.
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file + ".eps")  
> > &nbsp;&nbsp;&nbsp;&nbsp;end

---

<div class="post-metadata">

**Author:** ![Guest16](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Guest16](https://rubytalk.org/u/Guest16)\
**Post date:** [7 October 2007 13:56 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/4 "2007-10-07T13:56:47Z")

</div>

Nobuyoshi Nakada wrote:

> Hi,
> 
> At Sun, 7 Oct 2007 11:15:04 +0900,  
> Peter Bailey wrote in [ruby-talk:272952]:
> 
> > This always throws me. I keep getting "permission denied" messages when  
> > I simply try to rename files. Here's my code. The files in the directory  
> > start out without an extension.
> > 
> > Dir.chdir("F:/images")
> 
> Maybe on Windows?
> 
> > Dir.glob("\*").each do |file|  
> > &nbsp;&nbsp;File.new(file, "r").gets
> 
> This leaves the file still opened, and on Windows, it's  
> prohibited by the OS to move/rename files while they are  
> opened.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file + ".eps")  
> &nbsp;&nbsp;&nbsp;&nbsp;end

Yes, I'm on Windows. And, yes, it makes sense that it won't let me do it  
because the file is, apparently, still open. But, I guess I'm stumped as  
to how I can open the file to check that first line, match against that  
first line, close the file, and then be able to rename the filename of  
that particular file. Once the file is closed I really can't do anything  
more inside that same if-end loop, can I?

-Peter

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [7 October 2007 13:30 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/5 "2007-10-07T13:30:04Z")

</div>

Daniel Berger wrote:

> **···**
>
> > On Oct 7, 12:07 am, Nobuyoshi Nakada \<n...@ruby-lang.org\> wrote:
> 
> > \> if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}  
> > \> File.rename(file, file + ".eps")  
> > \> end
> > 
> > Why would he need to open the file at all?
> 
> Because he's checking the first line of the file, not the filename.
> 
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![7stud1](https://avatars.discourse-cdn.com/v4/letter/7/59ef9b/32.png) [@7stud1](https://rubytalk.org/u/7stud1)\
**Post date:** [7 October 2007 14:57 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/6 "2007-10-07T14:57:47Z")

</div>

Peter Bailey wrote:

> Nobuyoshi Nakada wrote:
> 
> > &nbsp;&nbsp;&nbsp;&nbsp;if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file + ".eps")  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> Yes, I'm on Windows. And, yes, it makes sense that it won't let me do it  
> because the file is, apparently, still open. But, I guess I'm stumped as  
> to how I can open the file to check that first line, match against that  
> first line, close the file, and then be able to rename the filename of  
> that particular file. Once the file is closed I really can't do anything  
> more inside that same if-end loop, can I?
> 
> -Peter

This line:

> > &nbsp;&nbsp;&nbsp;&nbsp;if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}

opens the file, gets the first line, closes the file, and checks the  
line against the regex for a match. If there is a match, this code  
executes:

> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file + ".eps")  
> > &nbsp;&nbsp;&nbsp;&nbsp;end

That line renames the file.

> But, I guess I'm stumped as  
> to how I can open the file to check that first line, match against that  
> first line, close the file, and then be able to rename the filename of  
> that particular file. Once the file is closed I really can't do anything  
> more inside that same if-end loop, can I?

What more do you want to do?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [7 October 2007 13:44 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/7 "2007-10-07T13:44:51Z")

</div>

Oops, my mistake.

Regards,

Dan

> **···**
>
> On Oct 7, 7:30 am, Sebastian Hungerecker \<sep...@googlemail.com\> wrote:
> 
> > Daniel Berger wrote:  
> > \> On Oct 7, 12:07 am, Nobuyoshi Nakada \<n...@ruby-lang.org\> wrote:  
> > \> \> if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}  
> > \> \> File.rename(file, file + ".eps")  
> > \> \> end
> > 
> > \> Why would he need to open the file at all?
> > 
> > Because he's checking the first line of the file, not the filename.

---

<div class="post-metadata">

**Author:** ![Guest16](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Guest16](https://rubytalk.org/u/Guest16)\
**Post date:** [7 October 2007 15:12 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/8 "2007-10-07T15:12:13Z")

</div>

> This line:
> 
> > > &nbsp;&nbsp;&nbsp;&nbsp;if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}
> 
> opens the file, gets the first line, closes the file, and checks the  
> line against the regex for a match. If there is a match, this code  
> executes:
> 
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file + ".eps")  
> > > &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> That line renames the file.

> What more do you want to do?

I want to rename the file(s), and, that's what's not working. I'm  
getting file permission errors.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [7 October 2007 16:19 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/9 "2007-10-07T16:19:01Z")

</div>

Peter Bailey wrote:

> \> This line:  
> \>\>\> if /%!PS-Adobe/ =~ IO.open(file) {|f| f.gets}  
> \>  
> \> opens the file, gets the first line, closes the file, and checks the  
> \> line against the regex for a match. If there is a match, this code  
> \>  
> \> executes:  
> \>\>\> File.rename(file, file + ".eps")  
> \>\>\> end  
> \>  
> \> That line renames the file.  
> \>  
> \> What more do you want to do?
> 
> I want to rename the file(s), and, that's what's not working. I'm  
> getting file permission errors.

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.  
Maybe you don't have permission to change the contents of the directory you  
are working on?

> **···**
>
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Jay\_Levitt1](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@Jay\_Levitt1](https://rubytalk.org/u/Jay_Levitt1)\
**Post date:** [7 October 2007 17:11 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/10 "2007-10-07T17:11:40Z")

</div>

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|  
&nbsp;&nbsp;if /%!PS-Adobe/ =~ open(file) {|f| f.gets}  
&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file + ".eps")  
&nbsp;&nbsp;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.

> **···**
>
> On Mon, 8 Oct 2007 01:19:01 +0900, Sebastian Hungerecker 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.
> 
> --  
> Jay Levitt |  
> Boston, MA | My character doesn't like it when they  
> Faster: jay at jay dot fm | cry or shout or hit.  
> [http://www.jay.fm](http://www.jay.fm) | - Kristoffer

---

<div class="post-metadata">

**Author:** ![Guest16](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Guest16](https://rubytalk.org/u/Guest16)\
**Post date:** [7 October 2007 22:40 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/11 "2007-10-07T22:40:42Z")

</div>

Jay Levitt 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|  
> &nbsp;&nbsp;if /%!PS-Adobe/ =~ open(file) {|f| f.gets}  
> &nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file + ".eps")  
> &nbsp;&nbsp;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

> **···**
>
> > On Mon, 8 Oct 2007 01:19:01 +0900, Sebastian Hungerecker wrote:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Guest16](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Guest16](https://rubytalk.org/u/Guest16)\
**Post date:** [7 October 2007 22:42 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/12 "2007-10-07T22:42:00Z")

</div>

"casual use of a block" not "black." Sorry.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jay\_Levitt1](https://avatars.discourse-cdn.com/v4/letter/j/ba9def/32.png) [@Jay\_Levitt1](https://rubytalk.org/u/Jay_Levitt1)\
**Post date:** [7 October 2007 23:10 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/13 "2007-10-07T23:10:05Z")

</div>

Me too! I've only been doing Ruby for a few years, and then only for play  
- just started being a pro-fessional this summer. Luckily, Ruby's so  
natural, you'll be giving newsgroup advice yourself in just a few months.

The "block trick" happens to be one of the first Ruby idioms I read about.  
In C++, people use RAII, Resource Acquisition Is Initialization, where you  
wrap a resource in an object, "open" the resource by creating the object,  
and "close" it by destroying the object.\*

In Ruby, though, the object gets destroyed by garbage collection, not by  
explicit destruction or stack unwinding. So RAII wouldn't work; you can  
never guarantee when (or if) the resource will be closed. Instead,  
resource-wrappers like File often let you specify a block; the wrapper  
opens the resource, yields to the block, and closes the resource for you.  
It's just a different wrapping idiom. If you haven't discovered  
[http://gotapi.com](http://gotapi.com) already, load up the Ruby tab and check out IO#open.

\* Wasn't RAII originally called RAIN, Resource Acquisition Is  
iNitialization? I could swear it was, and it certainly seems a more  
memorable choice for an acronym, but Google's not bearing me out on this.  
Then again, that was pre-Google. Anyone?

> **···**
>
> On Mon, 8 Oct 2007 07:40:42 +0900, Peter Bailey wrote:
> 
> > Now, I have to admit, I really need to look at your code, Jay, because,  
> > to me, it's like a different "dialect."
> 
> --  
> Jay Levitt |  
> Boston, MA | My character doesn't like it when they  
> Faster: jay at jay dot fm | cry or shout or hit.  
> [http://www.jay.fm](http://www.jay.fm) | - Kristoffer

---

<div class="post-metadata">

**Author:** ![7stud1](https://avatars.discourse-cdn.com/v4/letter/7/59ef9b/32.png) [@7stud1](https://rubytalk.org/u/7stud1)\
**Post date:** [7 October 2007 23:57 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/14 "2007-10-07T23:57:23Z")

</div>

Peter Bailey wrote:

> Now, I have to admit, I really need to look at your code, Jay, because,  
> to me, it's like a different "dialect."  
> ...  
> But, your  
> casual use of a black and opening a file here kind of blows me away,  
> actually.

1) First attempt at writing the code:

f = File.new(file)  
line1 = f.gets  
f.close

if /%!PS-Adobe/ =~ line1  
&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, "#{file}.eps")

2) Hmmm...I think I can make those first three lines shorter. If I use  
a File.open block that I read about in pickaxe, I won't have to call  
close():

line1 = nil  
File.open {|file| line1 = f.gets}

if /%!PS-Adobe/ =~ line1  
&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, "#{file}.eps")

3) And, shorter still...:

line1 = File.open {|file| f.gets}

if /%!PS-Adobe/ =~ line1  
&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, "#{file}.eps")

4) Hey, but look at 3). line1 appears on each line. So I can take what  
line1 is equal to on the first line and substitute it's value in the  
second line:

if /%!PS-Adobe/ =~ File.open {|file| f.gets}  
&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, "#{file}.eps")

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Phlip1](https://avatars.discourse-cdn.com/v4/letter/p/df788c/32.png) [@Phlip1](https://rubytalk.org/u/Phlip1)\
**Post date:** [7 October 2007 23:41 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/15 "2007-10-07T23:41:38Z")

</div>

Jay Levitt wrote:

> In Ruby, though, the object gets destroyed by garbage collection, not by  
> explicit destruction or stack unwinding. So RAII wouldn't work; you can  
> never guarantee when (or if) the resource will be closed. Instead,  
> resource-wrappers like File often let you specify a block; the wrapper  
> opens the resource, yields to the block, and closes the resource for you.  
> It's just a different wrapping idiom. If you haven't discovered  
> [http://gotapi.com](http://gotapi.com) already, load up the Ruby tab and check out IO#open.

A FAQ on the C++ mailing list is "shouldn't C++ learn from Java and add a 'finally' keyword"?

Block closures teach us that both RAII and 'finally' are hacks, and the Execute Around Pattern is the Real Deal.

> **···**
>
> --  
> &nbsp;&nbsp;Phlip

---

<div class="post-metadata">

**Author:** ![Guest16](https://avatars.discourse-cdn.com/v4/letter/g/779978/32.png) [@Guest16](https://rubytalk.org/u/Guest16)\
**Post date:** [8 October 2007 15:41 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/16 "2007-10-08T15:41:20Z")

</div>

> In Ruby, though, the object gets destroyed by garbage collection, not by  
> explicit destruction or stack unwinding. So RAII wouldn't work; you can  
> never guarantee when (or if) the resource will be closed. Instead,  
> resource-wrappers like File often let you specify a block; the wrapper  
> opens the resource, yields to the block, and closes the resource for  
> you.  
> It's just a different wrapping idiom. If you haven't discovered  
> [http://gotapi.com](http://gotapi.com) already, load up the Ruby tab and check out IO#open.
> 
> \* Wasn't RAII originally called RAIN, Resource Acquisition Is  
> iNitialization? I could swear it was, and it certainly seems a more  
> memorable choice for an acronym, but Google's not bearing me out on  
> this.  
> Then again, that was pre-Google. Anyone?

Well, I'd love to be able to give advice eventually in this forum, to  
someone even newbier than me. It's such a generous forum that I feel I  
have to give back in some way. . . .

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Simon\_Krahnke](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Simon\_Krahnke](https://rubytalk.org/u/Simon_Krahnke)\
**Post date:** [8 October 2007 17:35 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/17 "2007-10-08T17:35:03Z")

</div>

\* 7stud -- \<dolgun@excite.com\> (01:57) schrieb:

> if /%!PS-Adobe/ =~ File.open {|file| f.gets}  
> &nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, "#{file}.eps")

This is not C or Python, Ruby needs an end.

Or a one-liner:

> File.rename(file, "#{file}.eps") if /%!PS-Adobe/ =~ File.open {|file| f.gets}

mfg, simon .... l

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [8 October 2007 20:05 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/18 "2007-10-08T20:05:09Z")

</div>

I think that should be

File.rename(file, "#{file}.eps") if /%!PS-Adobe/ =~ File.open(file){|  
f\> f.gets}

More terse:

File.rename(file, "#{file}.eps") if /%!PS-Adobe/ =~ IO.read(file,10)

> **···**
>
> On Oct 8, 12:09 pm, Simon Krahnke \<overl...@gmx.li\> wrote:
> 
> > \* 7stud -- \<dol...@excite.com\> (01:57) schrieb:
> > 
> > \> if /%!PS-Adobe/ =~ File.open {|file| f.gets}  
> > \> File.rename(file, "#{file}.eps")
> > 
> > This is not C or Python, Ruby needs an end.
> > 
> > Or a one-liner:
> > 
> > \> File.rename(file, "#{file}.eps") if /%!PS-Adobe/ =~ File.open {|file| f.gets}

---

<div class="post-metadata">

**Author:** ![Simon\_Krahnke](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Simon\_Krahnke](https://rubytalk.org/u/Simon_Krahnke)\
**Post date:** [8 October 2007 22:35 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/19 "2007-10-08T22:35:14Z")

</div>

\* William James \<w\_a\_x\_man@yahoo.com\> (22:01) schrieb:

> > > File.rename(file, "#{file}.eps") if /%!PS-Adobe/ =~ File.open {|file| f.gets}
> 
> I think that should be
> 
> File.rename(file, "#{file}.eps") if /%!PS-Adobe/ =~ File.open(file){|  
> f\> f.gets}

Yeah, I correct one error and repeat another.

> More terse:
> 
> File.rename(file, "#{file}.eps") if /%!PS-Adobe/ =~ IO.read(file,10)

That's a violation of the DRY principle.

Define PS = '%!PS-Adobe' then

> File.rename(file, "#{file}.eps") if IO.read(file, PS.length)[PS]

mfg, simon .... l

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [9 October 2007 00:00 UTC](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207/20 "2007-10-09T00:00:06Z")

</div>

That's a violation of common sense. '%!PS-Adobe' isn't going  
to change, and therefore 10 won't need changing. Unjustified  
code inflation and a superfluous temporary variable.  
Furthermore, why say ".length" when you can say ".size"?  
You have a perverse, pronounced propensity for prolixity.

> **···**
>
> On Oct 8, 5:17 pm, Simon Krahnke \<overl...@gmx.li\> wrote:
> 
> > \* William James \<w\_a\_x\_...@yahoo.com\> (22:01) schrieb:
> > 
> > \>\>\> File.rename(file, "#{file}.eps") if /%!PS-Adobe/ =~ File.open {|file| f.gets}
> > 
> > \> I think that should be
> > 
> > \> File.rename(file, "#{file}.eps") if /%!PS-Adobe/ =~ File.open(file){|  
> > \> f\> f.gets}
> > 
> > Yeah, I correct one error and repeat another.
> > 
> > \> More terse:
> > 
> > \> File.rename(file, "#{file}.eps") if /%!PS-Adobe/ =~ IO.read(file,10)
> > 
> > That's a violation of the DRY principle.
> > 
> > Define PS = '%!PS-Adobe' then
> > 
> > \> File.rename(file, "#{file}.eps") if IO.read(file, PS.length)[PS]

[Next page](https://rubytalk.org/t/the-old-file-rename-not-working-again/41207.md?page=2)
