bwv549
February 23, 2009, 11:41pm
1
What is the most concise, bulletproof idiom for removing the file
extension from a file? Maybe 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:
filename.sub(/#{Regexp.escape(File.extname(filename))}$/, ‘’)
I use something like this when I want something quick:
filename.sub(/.\w$/,’’)
One could imagine doing something like this, but it is comical how
much code it takes:
File.join( File.dirname(filename), File.basename(filename,
File.extname(filename)))
What do you use for this (seemingly) trivial task?
Thanks,
John
bwv549
February 24, 2009, 12:03am
2
On Feb 23, 3:39 pm, bwv549 removed_email_address@domain.invalid wrote:
filename.sub(/.\w$/,‘’)
One could imagine doing something like this, but it is comical how
much code it takes:
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
bwv549
February 24, 2009, 12:38am
3
bwv549 wrote:
What is the most concise, bulletproof idiom for removing the file
extension from a file? Maybe 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:
filename.sub(/#{Regexp.escape(File.extname(filename))}$/, ‘’)
How about:
name = fname.chomp(File.extname(fname) )
bwv549
February 24, 2009, 12:52am
4
7stud – wrote:
How about:
name = fname.chomp(File.extname(fname) )
name = fname.chomp(File.extname(fname) ) <------
name = fname[/.*(?=…+$)/] <--------------
bwv549
February 24, 2009, 1:10am
5
All great answers - much better than what I was doing before.
To summarize then…
full pathname with no extension:
File.expand_path(File.basename(fname, File.extname(fname))
easy to remember and read:
fname.chomp(File.extname(fname) )
shortest:
fname[/.*(?=…+$)/]
Thanks all!
bwv549
February 24, 2009, 7:03am
6
On Tue, Feb 24, 2009 at 9:09 AM, bwv549 removed_email_address@domain.invalid wrote:
shortest:
  fname[/.*(?=..+$)/]
You can also use:
File.basename(‘foo.bar’, ‘.*’)
“foo”
^ manveru
bwv549
February 24, 2009, 10:00am
7
Michael F. removed_email_address@domain.invalid wrote:
On Tue, Feb 24, 2009 at 9:09 AM, bwv549 removed_email_address@domain.invalid wrote:
You can also use:
File.basename(‘foo.bar’, ‘.*’)
“foo”
Be aware if it gets the result you want:
File.basename(‘foo.tar.gz’, ‘.*’)
=> “foo.tar”
However,
Jan F.
bwv549
February 24, 2009, 3:35pm
8
On Tue, Feb 24, 2009 at 5:39 AM, bwv549 removed_email_address@domain.invalid wrote:
shortest:
fname[/.*(?=..+$)/]
Note that you’ve lost your robustness with that last:
irb> fname = ‘.vimrc’
=> “.vimrc”
irb> fname[/.*(?=..+$)/]
=> “”
irb> fname.chomp(File.extname(fname) )
=> “.vimrc”
martin
bwv549
March 6, 2009, 6:50am
9
Another summary is due:
Shortest, rock-solid idiom for removing the extension, without
removing the preceding path:
fname.chomp(File.extname(fname))
Best idiom if you are removing the path also:
File.basename(fname, ‘.*’)
It is unfortunate that these are so dissimilar, but both are easy to
remember and concise, so no problem. Thanks again for everyone’s help
on this.
bwv549
February 24, 2009, 4:08pm
10
full pathname with no extension:
File.expand_path(File.basename(fname, File.extname(fname))
File.basename(fname, ‘.*’) would be shorter. It also handles file names
like
“.vimrc” correctly.