# Idiomatic ruby version of this code?

**URL:** <https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253>\
**Category:** ruby-talk\
**Created:** [18 August 2005 16:24 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253 "2005-08-18T16:24:21Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brock\_Weaver](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Brock\_Weaver](https://rubytalk.org/u/Brock_Weaver)\
**Post date:** [18 August 2005 16:24 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253/1 "2005-08-18T16:24:21Z")

</div>

Showing off ruby to a coworker, and I want to emphasize how succinct yet  
clear it can be. So I want to rename all .mp3 files to .temp, or vice versa  
in the current directory -- in the shortest LOC \*that you would actually write\*;  
not the "here's a one-liner nobody in their right mind would every write if  
they had to maintain it".

Here's my first solution:

Dir.new("./").each do |file|  
&nbsp;&nbsp;if file.match(/mp3$/)  
&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file.gsub(/mp3$/, 'temp'))  
&nbsp;&nbsp;elsif file.match(/temp$/)  
&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file.gsub(/temp$/, 'mp3'))  
&nbsp;&nbsp;end  
end

Note you can NOT do the following, as it renames mp3 to temp,  
then immediately renames that same file back to mp3...

Dir.new("./").each do |file|  
&nbsp;&nbsp;File.rename(file, file.gsub(/mp3$/, 'temp')) if file.match(/mp3$/)  
&nbsp;&nbsp;File.rename(file, file.gsub(/temp$/, 'mp3')) if file.match(/temp$/)  
end

Any ideas?

> **···**
>
> --  
> Brock Weaver  
> [OBC]Technique

---

<div class="post-metadata">

**Author:** ![Brian\_Schroder1](https://avatars.discourse-cdn.com/v4/letter/b/2bfe46/32.png) [@Brian\_Schroder1](https://rubytalk.org/u/Brian_Schroder1)\
**Post date:** [18 August 2005 16:32 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253/2 "2005-08-18T16:32:29Z")

</div>

If it where a one time solution, I'd do something like this. Though  
maybe I'd extract the file endings into variables, to make it easier  
to change them.

mp3 = Dir['\*.mp3']  
temp = Dir['\*.temp']  
mp3.each do | fn | File.rename(fn, File.basename(fn, '.mp3') + '.temp') end  
temp.each do | fn | File.rename(fn, File.basename(fn, '.temp') + '.mp3') end

regards,

Brian

> **···**
>
> On 18/08/05, Brock Weaver \<brockweaver@gmail.com\> wrote:
> 
> > Showing off ruby to a coworker, and I want to emphasize how succinct yet  
> > clear it can be. So I want to rename all .mp3 files to .temp, or vice versa  
> > in the current directory -- in the shortest LOC \*that you would actually write\*;  
> > not the "here's a one-liner nobody in their right mind would every write if  
> > they had to maintain it".
> > 
> > Here's my first solution:
> > 
> > Dir.new("./").each do |file|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if file.match(/mp3$/)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file.gsub(/mp3$/, 'temp'))  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elsif file.match(/temp$/)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file.gsub(/temp$/, 'mp3'))  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > end
> > 
> > Note you can NOT do the following, as it renames mp3 to temp,  
> > then immediately renames that same file back to mp3...
> > 
> > Dir.new("./").each do |file|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file.gsub(/mp3$/, 'temp')) if file.match(/mp3$/)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file.gsub(/temp$/, 'mp3')) if file.match(/temp$/)  
> > end
> > 
> > Any ideas?
> > 
> > --  
> > Brock Weaver  
> > [OBC]Technique
> 
> --  
> [http://ruby.brian-schroeder.de/](http://ruby.brian-schroeder.de/)
> 
> Stringed instrument chords: [http://chordlist.brian-schroeder.de/](http://chordlist.brian-schroeder.de/)

---

<div class="post-metadata">

**Author:** ![Ben\_Giddings1](https://avatars.discourse-cdn.com/v4/letter/b/779978/32.png) [@Ben\_Giddings1](https://rubytalk.org/u/Ben_Giddings1)\
**Post date:** [18 August 2005 16:38 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253/3 "2005-08-18T16:38:47Z")

</div>

def switch\_endings(current\_ending, new\_ending)  
&nbsp;&nbsp;files = Dir.glob("\*.#{current\_ending}") do |filename|  
&nbsp;&nbsp;&nbsp;&nbsp;File.rename(filename, filename.gsub(/\.#{current\_ending}$/,  
".#{new\_ending}"))  
&nbsp;&nbsp;end  
end

As for doing both renames at the same time (i.e. swapping the various  
endings) you can't really do that without using a third temporary ending,  
so you could do:

switch\_endings("mp3", "mp3foo")  
switch\_endings("temp", "mp3")  
switch\_endings("mp3foo", "temp")

Ben

---

<div class="post-metadata">

**Author:** ![Berger\_Daniel1](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@Berger\_Daniel1](https://rubytalk.org/u/Berger_Daniel1)\
**Post date:** [18 August 2005 16:39 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253/4 "2005-08-18T16:39:09Z")

</div>

Brock Weaver wrote:

> Showing off ruby to a coworker, and I want to emphasize how succinct yet  
> clear it can be. So I want to rename all .mp3 files to .temp, or vice versa  
> in the current directory -- in the shortest LOC \*that you would actually write\*;  
> not the "here's a one-liner nobody in their right mind would every write if  
> they had to maintain it".
> 
> Here's my first solution:
> 
> Dir.new("./").each do |file|  
> &nbsp;&nbsp;if file.match(/mp3$/)  
> &nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file.gsub(/mp3$/, 'temp'))  
> &nbsp;&nbsp;elsif file.match(/temp$/)  
> &nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file.gsub(/temp$/, 'mp3'))  
> &nbsp;&nbsp;end  
> end

Dir["\*.mp3"].each{ |file|  
&nbsp;&nbsp;&nbsp;&nbsp;new\_name = File.basename(file, ".mp3") + ".temp"  
&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, new\_name)  
}

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![Zach\_Dennis1](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@Zach\_Dennis1](https://rubytalk.org/u/Zach_Dennis1)\
**Post date:** [18 August 2005 16:40 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253/5 "2005-08-18T16:40:11Z")

</div>

Brock Weaver wrote:

> Showing off ruby to a coworker, and I want to emphasize how succinct yet  
> clear it can be. So I want to rename all .mp3 files to .temp, or vice versa  
> in the current directory -- in the shortest LOC \*that you would actually write\*;  
> not the "here's a one-liner nobody in their right mind would every write if  
> they had to maintain it".
> 
> Here's my first solution:
> 
> Dir.new("./").each do |file|  
> &nbsp;&nbsp;if file.match(/mp3$/)  
> &nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file.gsub(/mp3$/, 'temp'))  
> &nbsp;&nbsp;elsif file.match(/temp$/)  
> &nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file.gsub(/temp$/, 'mp3'))  
> &nbsp;&nbsp;end  
> end
> 
> Note you can NOT do the following, as it renames mp3 to temp,  
> then immediately renames that same file back to mp3...
> 
> Dir.new("./").each do |file|  
> &nbsp;&nbsp;File.rename(file, file.gsub(/mp3$/, 'temp')) if file.match(/mp3$/)  
> &nbsp;&nbsp;File.rename(file, file.gsub(/temp$/, 'mp3')) if file.match(/temp$/)  
> end
> 
> Any ideas?

arr1, arr2 = Dir["\*.mp3"], Dir["\*.temp"]  
arr1.each { |f| File.rename( f, f.sub( /mp3$/, 'temp' ) ) }  
arr2.each { |f| File.rename( f, f.sub( /temp$/, 'mp3' ) ) }

Zach

---

<div class="post-metadata">

**Author:** ![why\_the\_lucky\_stiff1](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@why\_the\_lucky\_stiff1](https://rubytalk.org/u/why_the_lucky_stiff1)\
**Post date:** [18 August 2005 18:25 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253/6 "2005-08-18T18:25:07Z")

</div>

Brock Weaver wrote:

> Here's my first solution:
> 
> Dir.new("./").each do |file|  
> &nbsp;&nbsp;if file.match(/mp3$/)  
> &nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file.gsub(/mp3$/, 'temp'))  
> &nbsp;&nbsp;elsif file.match(/temp$/)  
> &nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file.gsub(/temp$/, 'mp3'))  
> &nbsp;&nbsp;end  
> end  
> &nbsp;&nbsp;

You could use the tertiary operator to switch file names, but the (exp .. and .. or) is also nice for this kind of swapping thing.

&nbsp;&nbsp;Dir["\*.{temp,mp3}"].each do |file|  
&nbsp;&nbsp;&nbsp;&nbsp;File.rename( file, file.gsub( /\w+$/ ) { |ext| ext == 'mp3' and 'temp' or 'mp3' } )  
&nbsp;&nbsp;end

If you don't want your extensions hardcoded everywhere, use an array:

&nbsp;&nbsp;exts = ['temp', 'mp3']  
&nbsp;&nbsp;Dir["\*.{#{exts.join ','}}"].each do |file|  
&nbsp;&nbsp;&nbsp;&nbsp;File.rename( file, file.gsub( /(\w+)$/ ) { exts.detect { |x| x != $1 } } )  
&nbsp;&nbsp;end

\_why

---

<div class="post-metadata">

**Author:** ![Mark\_Probert](https://avatars.discourse-cdn.com/v4/letter/m/d2c977/32.png) [@Mark\_Probert](https://rubytalk.org/u/Mark_Probert)\
**Post date:** [18 August 2005 22:19 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253/7 "2005-08-18T22:19:18Z")

</div>

Hi ..

> Showing off ruby to a coworker, and I want to emphasize how succinct yet  
> clear it can be. ...  
> Here's my first solution:
> 
> ...  
> Any ideas?

I know that this is kind of heretical, but in a situation like this, I'd  
rather use a shell script (I tend to inexpertly use zsh). In this case,  
the simple solution is something like:

$ a=(\*.mp3)  
$ for f in $a; do  
mv $f `basename $f .mp3`.temp  
done

Pretty easy to read (I'd say more so than the Ruby, IMO) and almost a  
one-liner. An easy idiom to remember, no editing required. If you need  
to reverse it, then up-arrow and change the lines (or use another  
variable, if needed).

However, if I had to do this as part of a larger project done in Ruby,  
then I would have no hesitation to take the Ruby route (and I have done  
almost exactly this at times).

My $0.02

Regards,

> **···**
>
> On Fri, 2005-08-19 at 01:24 +0900, Brock Weaver wrote:
> 
> --  
> -mark. (probertm at acm dot org)

---

<div class="post-metadata">

**Author:** ![Brock\_Weaver](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Brock\_Weaver](https://rubytalk.org/u/Brock_Weaver)\
**Post date:** [18 August 2005 16:40 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253/8 "2005-08-18T16:40:34Z")

</div>

Perfect! Thank you for your quick response. I decided to take the  
shortcut of parallel variable assignment too, so I'm violating my own  
rule a little I guess.

Anyway, thanks Brian.

> **···**
>
> On 8/18/05, Brian Schröder \<ruby.brian@gmail.com\> wrote:
> 
> > On 18/08/05, Brock Weaver \<brockweaver@gmail.com\> wrote:  
> > \> Showing off ruby to a coworker, and I want to emphasize how succinct yet  
> > \> clear it can be. So I want to rename all .mp3 files to .temp, or vice versa  
> > \> in the current directory -- in the shortest LOC \*that you would actually write\*;  
> > \> not the "here's a one-liner nobody in their right mind would every write if  
> > \> they had to maintain it".  
> > \>  
> > \> Here's my first solution:  
> > \>  
> > \> Dir.new("./").each do |file|  
> > \> if file.match(/mp3$/)  
> > \> File.rename(file, file.gsub(/mp3$/, 'temp'))  
> > \> elsif file.match(/temp$/)  
> > \> File.rename(file, file.gsub(/temp$/, 'mp3'))  
> > \> end  
> > \> end  
> > \>  
> > \> Note you can NOT do the following, as it renames mp3 to temp,  
> > \> then immediately renames that same file back to mp3...  
> > \>  
> > \> Dir.new("./").each do |file|  
> > \> File.rename(file, file.gsub(/mp3$/, 'temp')) if file.match(/mp3$/)  
> > \> File.rename(file, file.gsub(/temp$/, 'mp3')) if file.match(/temp$/)  
> > \> end  
> > \>  
> > \> Any ideas?  
> > \>  
> > \> --  
> > \> Brock Weaver  
> > \> [OBC]Technique  
> > \>  
> > \>
> > 
> > If it where a one time solution, I'd do something like this. Though  
> > maybe I'd extract the file endings into variables, to make it easier  
> > to change them.
> > 
> > mp3 = Dir['\*.mp3']  
> > temp = Dir['\*.temp']  
> > mp3.each do | fn | File.rename(fn, File.basename(fn, '.mp3') + '.temp') end  
> > temp.each do | fn | File.rename(fn, File.basename(fn, '.temp') + '.mp3') end
> > 
> > regards,
> > 
> > Brian
> > 
> > --  
> > [http://ruby.brian-schroeder.de/](http://ruby.brian-schroeder.de/)
> > 
> > Stringed instrument chords: [http://chordlist.brian-schroeder.de/](http://chordlist.brian-schroeder.de/)
> 
> --  
> Brock Weaver  
> [OBC]Technique

---

<div class="post-metadata">

**Author:** ![Berger\_Daniel1](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@Berger\_Daniel1](https://rubytalk.org/u/Berger_Daniel1)\
**Post date:** [18 August 2005 18:31 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253/9 "2005-08-18T18:31:41Z")

</div>

why the lucky stiff wrote:

> Brock Weaver wrote:
> 
> > Here's my first solution:
> > 
> > Dir.new("./").each do |file|  
> > &nbsp;&nbsp;&nbsp;&nbsp;if file.match(/mp3$/)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file.gsub(/mp3$/, 'temp'))  
> > &nbsp;&nbsp;&nbsp;&nbsp;elsif file.match(/temp$/)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, file.gsub(/temp$/, 'mp3'))  
> > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > end  
> > &nbsp;&nbsp;  
> > You could use the tertiary operator to switch file names, but the (exp .. and .. or) is also nice for this kind of swapping thing.
> 
> Dir["\*.{temp,mp3}"].each do |file|  
> &nbsp;&nbsp;&nbsp;File.rename( file, file.gsub( /\w+$/ ) { |ext| ext == 'mp3' and 'temp' or 'mp3' } )  
> end
> 
> If you don't want your extensions hardcoded everywhere, use an array:
> 
> exts = ['temp', 'mp3']  
> Dir["\*.{#{exts.join ','}}"].each do |file|  
> &nbsp;&nbsp;&nbsp;File.rename( file, file.gsub( /(\w+)$/ ) { exts.detect { |x| x != $1 } } )  
> end
> 
> \_why

gsub and backreferences? Why take that approach when File.basename is available?

def switch(old, new)  
&nbsp;&nbsp;&nbsp;&nbsp;Dir["\*.#{old}"].each{ |file|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_name = File.basename(file, old) + "." + new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, new\_name)  
&nbsp;&nbsp;&nbsp;&nbsp;}  
end

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![Gavin\_Kistner2](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@Gavin\_Kistner2](https://rubytalk.org/u/Gavin_Kistner2)\
**Post date:** [18 August 2005 18:36 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253/10 "2005-08-18T18:36:15Z")

</div>

Slightly more DRY (and yes, I'd use this myself):

mappings = { '.mp3' =\> '.temp', '.temp' =\> '.mp3' }  
mappings.each{ |orig\_ext, new\_ext|  
&nbsp;&nbsp;Dir["\*#{orig\_ext}"].each{ |filename|  
&nbsp;&nbsp;&nbsp;&nbsp;File.rename( filename, File.basename( filename, orig\_ext ) +  
new\_ext )  
&nbsp;&nbsp;}  
}

---

<div class="post-metadata">

**Author:** ![Guest15](https://avatars.discourse-cdn.com/v4/letter/g/6de8d8/32.png) [@Guest15](https://rubytalk.org/u/Guest15)\
**Post date:** [23 August 2005 19:45 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253/11 "2005-08-23T19:45:21Z")

</div>

Sorry for being late to the party (and wildly off topic for a ruby-talk thread).

> Hi ..
> 
> I know that this is kind of heretical, but in a situation like this, I'd  
> rather use a shell script (I tend to inexpertly use zsh). In this case,  
> the simple solution is something like:
> 
> $ a=(\*.mp3)  
> $ for f in $a; do  
> mv $f `basename $f .mp3`.temp  
> done

(BTW, my local zsh doesn't like basename.)

This doesn't really answer the initial requirement though, here's  
an idiomatic bash version ... although the variable mangling's a  
neat trick, a lot of folks here will probably find it too much like  
line noise.

MP3=`echo *.mp3`  
TEMP=`echo *.temp`  
for file in $MP3; do mv $file ${file%.mp3}.temp; done  
for file in $TEMP; do mv $file ${file%.temp}.mp3; done

it's still not safe in terms of overwriting existing files, and there's  
no question in my mind that the Ruby versions are prettier.

> **···**
>
> On 8/18/05, Mark Probert \<probertm@acm.org\> wrote:
> 
> > Pretty easy to read (I'd say more so than the Ruby, IMO) and almost a  
> > one-liner. An easy idiom to remember, no editing required. If you need  
> > to reverse it, then up-arrow and change the lines (or use another  
> > variable, if needed).
> > 
> > However, if I had to do this as part of a larger project done in Ruby,  
> > then I would have no hesitation to take the Ruby route (and I have done  
> > almost exactly this at times).
> > 
> > My $0.02
> > 
> > Regards,
> > 
> > --  
> > -mark. (probertm at acm dot org)
> 
> --  
> thanks,  
> -pate  
> -------------------------  
> We are often unable to tell people what they need to know, because  
> they want to know something else, and would therefore only  
> misunderstand what we said  
> - the Raven (George MacDonald, Lilith)

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [18 August 2005 18:34 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253/12 "2005-08-18T18:34:03Z")

</div>

Brock Weaver schrieb:

> Perfect! Thank you for your quick response. I decided to take the  
> shortcut of parallel variable assignment too, so I'm violating my own  
> rule a little I guess.

Note that all the offered solutions including your original version don't work if there are two files named "x.mp3" and "x.temp".

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![why\_the\_lucky\_stiff1](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@why\_the\_lucky\_stiff1](https://rubytalk.org/u/why_the_lucky_stiff1)\
**Post date:** [18 August 2005 18:46 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253/13 "2005-08-18T18:46:20Z")

</div>

Daniel Berger wrote:

> gsub and backreferences? Why take that approach when File.basename is available?
> 
> def switch(old, new)  
> &nbsp;&nbsp;&nbsp;Dir["\*.#{old}"].each{ |file|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_name = File.basename(file, old) + "." + new  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, new\_name)  
> &nbsp;&nbsp;&nbsp;}  
> end

You understand he's trying to swap extensions, right? (I think?) Well, you've got to get the extension name. And File.extname( file )[1..-1] is ugly.

I always go for short and dirty. I steal second base!

\_why

---

<div class="post-metadata">

**Author:** ![Berger\_Daniel1](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@Berger\_Daniel1](https://rubytalk.org/u/Berger_Daniel1)\
**Post date:** [18 August 2005 18:36 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253/14 "2005-08-18T18:36:30Z")

</div>

Pit Capitain wrote:

> Brock Weaver schrieb:
> 
> > Perfect! Thank you for your quick response. I decided to take the  
> > shortcut of parallel variable assignment too, so I'm violating my own  
> > rule a little I guess.
> 
> Note that all the offered solutions including your original version don't work if there are two files named "x.mp3" and "x.temp".
> 
> Regards,  
> Pit

def switch(old, new)  
&nbsp;&nbsp;&nbsp;&nbsp;Dir["\*.#{old}"].each{ |file|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_name = File.basename(file, old) + "." + new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, new\_name) unless File.exists?(new\_name)  
&nbsp;&nbsp;&nbsp;&nbsp;}  
end

Regards,

Dan

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [18 August 2005 18:38 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253/15 "2005-08-18T18:38:45Z")

</div>

Pit Capitain schrieb:

> Brock Weaver schrieb:
> 
> > Perfect! Thank you for your quick response. I decided to take the  
> > shortcut of parallel variable assignment too, so I'm violating my own  
> > rule a little I guess.
> 
> Note that all the offered solutions including your original version don't work if there are two files named "x.mp3" and "x.temp".

Sorry, Ben's version works if you choose a non-existing temporary ending.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Berger\_Daniel1](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@Berger\_Daniel1](https://rubytalk.org/u/Berger_Daniel1)\
**Post date:** [18 August 2005 19:17 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253/16 "2005-08-18T19:17:53Z")

</div>

why the lucky stiff wrote:

> Daniel Berger wrote:
> 
> > gsub and backreferences? Why take that approach when File.basename is available?
> > 
> > def switch(old, new)  
> > &nbsp;&nbsp;&nbsp;Dir["\*.#{old}"].each{ |file|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;new\_name = File.basename(file, old) + "." + new  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, new\_name)  
> > &nbsp;&nbsp;&nbsp;}  
> > end
> 
> You understand he's trying to swap extensions, right? (I think?) Well, you've got to get the extension name. And File.extname( file )[1..-1] is ugly.
> 
> I always go for short and dirty. I steal second base!
> 
> \_why

Oh, he wants to switch both. Hm, ok. Still no gsub or backreferences:

djberge@~/programming/ruby/temp-670\>cat switcher.rb  
Dir["\*.{mp3,temp}"].each{ |file|  
&nbsp;&nbsp;&nbsp;&nbsp;ext = File.extname(file) == ".mp3" ? ".temp" : ".mp3"  
&nbsp;&nbsp;&nbsp;&nbsp;new\_name = File.basename(file, ".\*") + ext  
&nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, new\_name)  
}  
djberge@~/programming/ruby/temp-671\>ls -l  
total 2  
-rw-r--r-- 1 djberge staff 0 Aug 18 13:08 bar.temp  
-rw-r--r-- 1 djberge staff 0 Aug 18 13:08 baz.mp3  
-rw-r--r-- 1 djberge staff 0 Aug 18 13:09 blah.nada  
-rw-r--r-- 1 djberge staff 0 Aug 18 13:08 foo.mp3  
-rw-r--r-- 1 djberge staff 169 Aug 18 13:15 switcher.rb  
djberge@~/programming/ruby/temp-672\>ruby switcher.rb  
djberge@~/programming/ruby/temp-673\>ls -l  
total 2  
-rw-r--r-- 1 djberge staff 0 Aug 18 13:08 bar.mp3  
-rw-r--r-- 1 djberge staff 0 Aug 18 13:08 baz.temp  
-rw-r--r-- 1 djberge staff 0 Aug 18 13:09 blah.nada  
-rw-r--r-- 1 djberge staff 0 Aug 18 13:08 foo.temp  
-rw-r--r-- 1 djberge staff 169 Aug 18 13:15 switcher.rb

---

<div class="post-metadata">

**Author:** ![Brock\_Weaver](https://avatars.discourse-cdn.com/v4/letter/b/7ab992/32.png) [@Brock\_Weaver](https://rubytalk.org/u/Brock_Weaver)\
**Post date:** [18 August 2005 19:36 UTC](https://rubytalk.org/t/idiomatic-ruby-version-of-this-code/20253/17 "2005-08-18T19:36:38Z")

</div>

Thank you all for your replies. I've chosen to use a cross between  
Zach's and Brian's implementations.

arr1, arr2 = Dir["\*.mp3"], Dir["\*.temp"]  
arr1.each do | fn | File.rename(fn, File.basename(fn, '.mp3') + '.temp') end  
arr2.each do | fn | File.rename(fn, File.basename(fn, '.temp') + '.mp3') end

Zach's is both concise and clear -- I don't need flexibility per se;  
hardcoding extensions is fine. Brian's use of File.basename instead  
of regex made the script run roughly 5x faster on my XP box (I'm  
renaming about 2,500 files).

> **···**
>
> On 8/18/05, Daniel Berger \<Daniel.Berger@qwest.com\> wrote:
> 
> > why the lucky stiff wrote:  
> > \> Daniel Berger wrote:  
> > \>  
> > \>\> gsub and backreferences? Why take that approach when File.basename is  
> > \>\> available?  
> > \>\>  
> > \>\> def switch(old, new)  
> > \>\> Dir["\*.#{old}"].each{ |file|  
> > \>\> new\_name = File.basename(file, old) + "." + new  
> > \>\> File.rename(file, new\_name)  
> > \>\> }  
> > \>\> end  
> > \>  
> > \> You understand he's trying to swap extensions, right? (I think?) Well,  
> > \> you've got to get the extension name. And File.extname( file )[1..-1]  
> > \> is ugly.  
> > \>  
> > \> I always go for short and dirty. I steal second base!  
> > \>  
> > \> \_why
> > 
> > Oh, he wants to switch both. Hm, ok. Still no gsub or backreferences:
> > 
> > djberge@~/programming/ruby/temp-670\>cat switcher.rb  
> > Dir["\*.{mp3,temp}"].each{ |file|  
> > &nbsp;&nbsp;&nbsp;&nbsp;ext = File.extname(file) == ".mp3" ? ".temp" : ".mp3"  
> > &nbsp;&nbsp;&nbsp;&nbsp;new\_name = File.basename(file, ".\*") + ext  
> > &nbsp;&nbsp;&nbsp;&nbsp;File.rename(file, new\_name)  
> > }  
> > djberge@~/programming/ruby/temp-671\>ls -l  
> > total 2  
> > -rw-r--r-- 1 djberge staff 0 Aug 18 13:08 bar.temp  
> > -rw-r--r-- 1 djberge staff 0 Aug 18 13:08 baz.mp3  
> > -rw-r--r-- 1 djberge staff 0 Aug 18 13:09 blah.nada  
> > -rw-r--r-- 1 djberge staff 0 Aug 18 13:08 foo.mp3  
> > -rw-r--r-- 1 djberge staff 169 Aug 18 13:15 switcher.rb  
> > djberge@~/programming/ruby/temp-672\>ruby switcher.rb  
> > djberge@~/programming/ruby/temp-673\>ls -l  
> > total 2  
> > -rw-r--r-- 1 djberge staff 0 Aug 18 13:08 bar.mp3  
> > -rw-r--r-- 1 djberge staff 0 Aug 18 13:08 baz.temp  
> > -rw-r--r-- 1 djberge staff 0 Aug 18 13:09 blah.nada  
> > -rw-r--r-- 1 djberge staff 0 Aug 18 13:08 foo.temp  
> > -rw-r--r-- 1 djberge staff 169 Aug 18 13:15 switcher.rb
> 
> --  
> Brock Weaver  
> [OBC]Technique
