"Yvon Thoraval" <yvon.thoravalNO-SPAM / free.fr> schrieb im Newsbeitrag
news:1gbijxj.jnr3jyxfhxp6N%yvon.thoravalNO-SPAM / free.fr...
> Robert Klemme <bob.news / gmx.net> wrote:
>
> > The error is easily explained by the behavior of split:
> >
> > irb(main):010:0> ar = "/foo/bar".split '/'
> > => ["", "foo", "bar"]
> > irb(main):011:0> ar.join '/'
> > => "/foo/bar"
> >
> > Note the empty element at the beginning. compact() doesn't change that
> > since it removes only nils:
> >
> > irb(main):013:0> ar = "/foo/bar".split( '/' ).compact
> > => ["", "foo", "bar"]
> > irb(main):014:0> ar.join '/'
> > => "/foo/bar"
> >
> > However, File.split and File.join are used much easier and I guess
they
> > are more appropriate in your case.
>
> ok, tanxs, however my pathes are comin' from java-side (i'm using
> JRuby), the reason i didn't make use of File.[split|join],

Why is that a reason to not use File.split and join?  Are those paths
formatted differently?  Normally Java uses the formatting of the platform
which Ruby does, too.  The only case where they differ is a Windows box
with Ruby on cygwin IMHO.

> i've changed
> #compact to #delete("") and works fine

I assume you changed it in class Array.  That's not a good idea since it
can break much code that depends on the standard functionality.  You can
achieve the same much easier and less intrusive:

irb(main):001:0> ar = "/foo/bar".split( '/' ).select {|x| x.length > 0}
=> ["foo", "bar"]
irb(main):002:0> ar = "/foo/bar".split( '/' ).select {|x| x != ""}
=> ["foo", "bar"]

Or, to be sure to exlude possible Nils:

irb(main):003:0> ar = "/foo/bar".split( '/' ).select {|x| x && x.length >
0}
=> ["foo", "bar"]
irb(main):004:0> ar = "/foo/bar".split( '/' ).select {|x| x && x != ""}
=> ["foo", "bar"]

Joining then yields:

irb(main):005:0> ar.join '/'
=> "foo/bar"

Regards

    robert