Forum: Ruby what is best idiom to remove file extension

Posted by bwv549 (Guest)
on 2009-02-23 23:41
(Received via mailing list)
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
Posted by Daniel Berger (djberg96)
on 2009-02-24 00:03
(Received via mailing list)
On Feb 23, 3:39 pm, bwv549 <jtpri...@gmail.com> 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
Posted by 7stud -- (7stud)
on 2009-02-24 00:38
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) )
Posted by 7stud -- (7stud)
on 2009-02-24 00:52
7stud -- wrote:
>
> How about:
> 
> name = fname.chomp(File.extname(fname) )

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

name = fname[/.*(?=\..+$)/] <--------------
Posted by bwv549 (Guest)
on 2009-02-24 01:10
(Received via mailing list)
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!
Posted by Michael Fellinger (Guest)
on 2009-02-24 07:03
(Received via mailing list)
On Tue, Feb 24, 2009 at 9:09 AM, bwv549 <jtprince@gmail.com> wrote:
> shortest:
>    fname[/.*(?=\..+$)/]

You can also use:

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

^ manveru
Posted by Jan Friedrich (janfri)
on 2009-02-24 10:00
(Received via mailing list)
Michael Fellinger <m.fellinger@gmail.com> wrote:

> On Tue, Feb 24, 2009 at 9:09 AM, bwv549 <jtprince@gmail.com> 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 Friedrich
Posted by Martin DeMello (Guest)
on 2009-02-24 15:35
(Received via mailing list)
On Tue, Feb 24, 2009 at 5:39 AM, bwv549 <jtprince@gmail.com> 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
Posted by -lim- (Guest)
on 2009-02-24 16:08
(Received via mailing list)
>
> > 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.
Posted by bwv549 (Guest)
on 2009-03-06 06:50
(Received via mailing list)
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.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.