what is best idiom to remove file extension

Discussion in 'Ruby' started by bwv549, Feb 23, 2009.

  1. bwv549

    bwv549 Guest

    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, Feb 23, 2009
    #1
    1. Advertising

  2. On Feb 23, 3:39=A0pm, bwv549 <> 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
     
    Daniel Berger, Feb 23, 2009
    #2
    1. Advertising

  3. bwv549

    7stud -- Guest

    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 via http://www.ruby-forum.com/.
     
    7stud --, Feb 23, 2009
    #3
  4. bwv549

    7stud -- Guest

    7stud -- wrote:
    >
    > How about:
    >
    > name = fname.chomp(File.extname(fname) )


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

    name = fname[/.*(?=\..+$)/] <--------------
    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Feb 23, 2009
    #4
  5. bwv549

    bwv549 Guest

    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, Feb 24, 2009
    #5
  6. On Tue, Feb 24, 2009 at 9:09 AM, bwv549 <> wrote:
    > 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
     
    Michael Fellinger, Feb 24, 2009
    #6
  7. Michael Fellinger <> wrote:

    > On Tue, Feb 24, 2009 at 9:09 AM, bwv549 <> 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
     
    Jan Friedrich, Feb 24, 2009
    #7
  8. On Tue, Feb 24, 2009 at 5:39 AM, bwv549 <> wrote:
    > 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
     
    Martin DeMello, Feb 24, 2009
    #8
  9. bwv549

    -lim- Guest

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

    >
    > > 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.



    --
    The end is here -->
     
    -lim-, Feb 24, 2009
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon Perez
    Replies:
    1
    Views:
    271
    David M. Cooke
    Apr 7, 2004
  2. Robert Brewer
    Replies:
    1
    Views:
    506
    bsmith
    Nov 7, 2004
  3. Steve
    Replies:
    3
    Views:
    27,030
    Steve
    Aug 25, 2006
  4. Nan Li
    Replies:
    2
    Views:
    1,076
    Andrew Koenig
    Nov 8, 2005
  5. Öö Tiib
    Replies:
    0
    Views:
    752
    Öö Tiib
    Jun 16, 2010
Loading...

Share This Page