Exchanging the extension in a filename

R

Ronald Fischer

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 <[email protected]>
Phone: +49-89-452133-162
 
S

Simon Krahnke

* Ronald Fischer said:
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
 
W

William James

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

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

Ronald Fischer

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
=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
 
R

Ronald Fischer

* Ronald Fischer said:
=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.
=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
 
R

rio4ruby

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



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/
 
R

Robert Klemme

2007/8/14 said:
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

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
 
J

Jim Weirich

Ronald said:
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).
 

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,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top