[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Exchanging the extension in a filename

Ronald Fischer

8/14/2007 10:06:00 AM

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 <ronald.fischer@venyon.com>
Phone: +49-89-452133-162

7 Answers

Simon Krahnke

8/14/2007 2:32:00 PM

0

* Ronald Fischer <ronald.fischer@venyon.com> (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

William James

8/14/2007 5:55:00 PM

0

On Aug 14, 5:05 am, "Ronald Fischer" <ronald.fisc...@venyon.com>
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 <ronald.fisc...@venyon.com>
> Phone: +49-89-452133-162

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

Ronald Fischer

8/16/2007 7:54:00 AM

0

> > 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 <ronald.fisc...@venyon.com>
> > Phone: +49-89-452133-162
>
> 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


Ronald Fischer

8/16/2007 7:58:00 AM

0

> * Ronald Fischer <ronald.fischer@venyon.com> (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

rio4ruby

8/16/2007 8:43:00 AM

0

On Aug 16, 12:57 am, "Ronald Fischer" <ronald.fisc...@venyon.com>
wrote:
> > * Ronald Fischer <ronald.fisc...@venyon.com> (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.blo...


Robert Klemme

8/16/2007 2:21:00 PM

0

2007/8/14, William James <w_a_x_man@yahoo.com>:
> On Aug 14, 5:05 am, "Ronald Fischer" <ronald.fisc...@venyon.com>
> 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 <ronald.fisc...@venyon.com>
> > 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

Jim Weirich

8/16/2007 5:10:00 PM

0

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