Forum: Ruby what is best idiom to remove file extension

883ee4d990ef9e57a79b5b0ff3b66241?d=identicon&s=25 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
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 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
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 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) )
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 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[/.*(?=\..+$)/] <--------------
883ee4d990ef9e57a79b5b0ff3b66241?d=identicon&s=25 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!
86e33dee4a89a8879a26487051c216a8?d=identicon&s=25 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
B32a1911d783e0a95e0b062fd5d0b64b?d=identicon&s=25 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
Ae16cb4f6d78e485b04ce1e821592ae5?d=identicon&s=25 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
87ef5d1e14b148eb596433bc17ffe690?d=identicon&s=25 -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.
883ee4d990ef9e57a79b5b0ff3b66241?d=identicon&s=25 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

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.