From: Robert Klemme Date: 2007-08-16T23:20:30+09:00 Subject: Re: Exchanging the extension in a filename 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