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
On 8/14/07, Ronald F. [email protected] wrote:
This works fine, but I wonder whether there isn’t a more
elegant way to do it.
sub is pretty elegant.
If you wanted you could do it like File.join(File.dirname(filename),
File.basename(filename, “.java”) + “.class”)
Ronald
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
On Aug 14, 5:05 am, “Ronald F.” [email protected]
wrote:
elegant way to do it.
Ronald
Ronald F. [email protected]
Phone: +49-89-452133-162
filename = “abc/x.java”
==>“abc/x.java”
filename[ /java$/ ] = “class”
==>“class”
filename
==>“abc/x.class”
filename = “abc/x.java”
==>“abc/x.java”
filename[ /java$/ ] = “class”
==>“class”
filename
==>“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
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
On Aug 16, 12:57 am, “Ronald F.” [email protected]
wrote:
This works fine, but I wonder whether there isn’t a more
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,
Ronald F. 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 W.
2007/8/14, William J. [email protected]:
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