Daniel Berger
2/23/2009 11:02:00 PM
On Feb 23, 3:39=A0pm, bwv549 <jtpri...@gmail.com> wrote:
> What is the most concise, bulletproof idiom for removing the file
> extension from a file? =A0Maybe I've missed something obvious that
> everyone else uses...
>
> Here's what I usually do:
>
> This is what I usually use if I want bulletproof:
> =A0 filename.sub(/#{Regexp.escape(File.extname(filename))}$/, '')
>
> I use something like this when I want something quick:
> =A0 filename.sub(/\.\w$/,'')
>
> One could imagine doing something like this, but it is comical how
> much code it takes:
> =A0 File.join( File.dirname(filename), File.basename(filename,
> File.extname(filename)))
Don't forget about the 2nd argument to File.basename:
File.basename(file, File.extname(file))
If you need to guarantee the full path:
File.expand_path(File.basename(file, File.extname(file))
Regards,
Dan