Exchanging the extension in a filename

Discussion in 'Ruby' started by Ronald Fischer, Aug 14, 2007.

  1. I have a string representing the name of a java file. I would
    like to get a new string, where the extension .java has been
    replaced by .class.

    My approach was to use String#sub:

    filename=3D"abc/x.java"
    filename.sub(/java$/,'class')=20

    This works fine, but I wonder whether there isn't a more
    elegant way to do it.

    Ronald
    --=20
    Ronald Fischer <>
    Phone: +49-89-452133-162
    Ronald Fischer, Aug 14, 2007
    #1
    1. Advertising

  2. * Ronald Fischer <> (12:05) schrieb:

    > filename.sub(/java$/,'class')


    I'd match the point too, not everything that ends with "java" is java
    source file: filename.sub(/\.java$/, '.class')

    > This works fine, but I wonder whether there isn't a more
    > elegant way to do it.


    What is unelegant about that?

    mfg, simon .... l
    Simon Krahnke, Aug 14, 2007
    #2
    1. Advertising

  3. On Aug 14, 5:05 am, "Ronald Fischer" <>
    wrote:
    > I have a string representing the name of a java file. I would
    > like to get a new string, where the extension .java has been
    > replaced by .class.
    >
    > My approach was to use String#sub:
    >
    > filename="abc/x.java"
    > filename.sub(/java$/,'class')
    >
    > This works fine, but I wonder whether there isn't a more
    > elegant way to do it.
    >
    > Ronald
    > --
    > Ronald Fischer <>
    > Phone: +49-89-452133-162


    filename = "abc/x.java"
    ==>"abc/x.java"
    filename[ /java$/ ] = "class"
    ==>"class"
    filename
    ==>"abc/x.class"
    William James, Aug 14, 2007
    #3
  4. > > filename=3D"abc/x.java"
    > > filename.sub(/java$/,'class')
    > >
    > > This works fine, but I wonder whether there isn't a more
    > > elegant way to do it.
    > >
    > > Ronald
    > > --
    > > Ronald Fischer <>
    > > Phone: +49-89-452133-162

    >=20
    > filename =3D "abc/x.java"
    > =3D=3D>"abc/x.java"
    > filename[ /java$/ ] =3D "class"
    > =3D=3D>"class"
    > filename
    > =3D=3D>"abc/x.class"


    Thanks - this is a nice alternative (for the case I actually
    want to modify filename). Of course it assumes that I know
    that the file ends in .java (but this is true in my case anyway).

    Ronald
    =20
    Ronald Fischer, Aug 16, 2007
    #4
  5. > * Ronald Fischer <> (12:05) schrieb:
    >=20
    > > filename.sub(/java$/,'class')=20

    >=20
    > I'd match the point too, not everything that ends with "java" is java
    > source file: filename.sub(/\.java$/, '.class')=20


    In general, yes, but in my case I *know* that the file ends in .java
    anyway, so matching the dot is not so important.

    > > This works fine, but I wonder whether there isn't a more
    > > elegant way to do it.

    >=20
    > What is unelegant about that?


    Maybe I did too much shell programming before ;-)

    On shell level, I would do a=20

    $(dirname $FILENAME)/($basename $FILENAME java)class

    so I was thinking whether I might have overlooked some useful method
    in the FilePath or File class which would be helpful here.

    Thanks for the advice...

    Ronald
    Ronald Fischer, Aug 16, 2007
    #5
  6. Ronald Fischer

    rio4ruby Guest

    On Aug 16, 12:57 am, "Ronald Fischer" <>
    wrote:
    > > * Ronald Fischer <> (12:05) schrieb:

    >
    > > > filename.sub(/java$/,'class')

    >
    > > I'd match the point too, not everything that ends with "java" is java
    > > source file: filename.sub(/\.java$/, '.class')

    >
    > In general, yes, but in my case I *know* that the file ends in .java
    > anyway, so matching the dot is not so important.
    >
    > > > This works fine, but I wonder whether there isn't a more
    > > > elegant way to do it.

    >
    > > What is unelegant about that?

    >
    > Maybe I did too much shell programming before ;-)
    >
    > On shell level, I would do a
    >
    > $(dirname $FILENAME)/($basename $FILENAME java)class
    >
    > so I was thinking whether I might have overlooked some useful method
    > in the FilePath or File class which would be helpful here.
    >
    > Thanks for the advice...
    >
    > Ronald


    If what you are looking for is a way to change the extension of some
    ".java' files
    to have the extension ".class", you might try Rio (http://
    rio.rubyforge.org)

    require 'rio'
    rio('abc').rename files('*.java') do |file|
    file.extname = '.class'
    end

    If you want to include files in subdirectories of 'abc' use:

    rio('abc').rename.all.files('*.java') do |file|
    file.extname = '.class'
    end

    Hope this helps,
    http://rio4ruby.blogspot.com/
    rio4ruby, Aug 16, 2007
    #6
  7. 2007/8/14, William James <>:
    > On Aug 14, 5:05 am, "Ronald Fischer" <>
    > wrote:
    > > I have a string representing the name of a java file. I would
    > > like to get a new string, where the extension .java has been
    > > replaced by .class.
    > >
    > > My approach was to use String#sub:
    > >
    > > filename="abc/x.java"
    > > filename.sub(/java$/,'class')
    > >
    > > This works fine, but I wonder whether there isn't a more
    > > elegant way to do it.
    > >
    > > Ronald
    > > --
    > > Ronald Fischer <>
    > > Phone: +49-89-452133-162

    >
    > filename = "abc/x.java"
    > ==>"abc/x.java"
    > filename[ /java$/ ] = "class"
    > ==>"class"
    > filename
    > ==>"abc/x.class"


    I'd rather include the dot to avoid renaming files called "foo.notjava"

    irb(main):001:0> f="foo/bar.java"
    => "foo/bar.java"
    irb(main):002:0> f[/\.java$/]=".class"
    => ".class"
    irb(main):003:0> f
    => "foo/bar.class"

    Kind regards

    robert
    Robert Klemme, Aug 16, 2007
    #7
  8. Ronald Fischer

    Jim Weirich Guest

    Ronald Fischer wrote:
    > I have a string representing the name of a java file. I would
    > like to get a new string, where the extension .java has been
    > replaced by .class.


    [...]

    > This works fine, but I wonder whether there isn't a more
    > elegant way to do it.


    If you are *really* lazy:

    require 'rake' # NOTE: Not running rake, just using its library.

    java_file = "filename.java"
    class_file = java_file.ext("class")

    If you have a bunch of files that need this, you can do:

    require 'rake'

    java_files = FileList['src/**/*.java']
    class_files = java_files.ext("class")

    If you need more extensive filename manipulation, check out the
    "pathmap" method (available on both FileLists and Strings when using
    rake).

    --
    -- Jim Weirich
    --
    Posted via http://www.ruby-forum.com/.
    Jim Weirich, Aug 16, 2007
    #8
    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. Rafael Veronezi
    Replies:
    0
    Views:
    395
    Rafael Veronezi
    Jul 2, 2003
  2. Gianni

    Exchanging results

    Gianni, Jan 11, 2005, in forum: Java
    Replies:
    2
    Views:
    358
    Peter Kirk
    Jan 11, 2005
  3. Gianni

    Exchanging results

    Gianni, Jan 12, 2005, in forum: Java
    Replies:
    4
    Views:
    300
    John C. Bollinger
    Jan 12, 2005
  4. Peter van Schie
    Replies:
    2
    Views:
    396
    Peter van Schie
    Oct 2, 2005
  5. Amol
    Replies:
    2
    Views:
    430
    Valentin Tihomirov
    Oct 12, 2003
Loading...

Share This Page