what is best idiom to remove file extension

B

bwv549

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
 
D

Daniel Berger

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
 
7

7stud --

bwv549 said:
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) )
 
7

7stud --

7stud said:
How about:

name = fname.chomp(File.extname(fname) )

name = fname.chomp(File.extname(fname) ) <------

name = fname[/.*(?=\..+$)/] <--------------
 
B

bwv549

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!
 
M

Michael Fellinger

All great answers - much better than what I was doing before.

To summarize then...

full pathname with no extension:
=C2=A0 =C2=A0File.expand_path(File.basename(fname, File.extname(fname))

easy to remember and read:
=C2=A0 =C2=A0fname.chomp(File.extname(fname) )

shortest:
=C2=A0 =C2=A0fname[/.*(?=3D\..+$)/]

You can also use:

File.basename('foo.bar', '.*')
# "foo"

^ manveru
 
J

Jan Friedrich

Michael Fellinger said:
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 Friedrich
 
M

Martin DeMello

All great answers - much better than what I was doing before.

To summarize then...

full pathname with no extension:
=A0 =A0File.expand_path(File.basename(fname, File.extname(fname))

easy to remember and read:
=A0 =A0fname.chomp(File.extname(fname) )

shortest:
=A0 =A0fname[/.*(?=3D\..+$)/]

Note that you've lost your robustness with that last:

irb> fname =3D '.vimrc'
=3D> ".vimrc"
irb> fname[/.*(?=3D\..+$)/]
=3D> ""
irb> fname.chomp(File.extname(fname) )
=3D> ".vimrc"

martin
 
L

-lim-

[Note: parts of this message were removed to make it a legal post.]


File.basename(fname, '.*') would be shorter. It also handles file names like
".vimrc" correctly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top