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|
>     ext = File.extname(file) == ".mp3" ? ".temp" : ".mp3"
>     new_name = File.basename(file, ".*") + ext
>     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