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