[newbie] prob with join "/"

i get a very small prob with join :

(fs stands for file.separator)

plary = Array.new
plary << context.split(fs).compact << path.split(fs).compact <<
“languages” << “bottles”

print "size = ", plary.size, “\n” # -> size = 4

plary.each {|p| print p, “\n” } # -> com-ultrid-yvonthor
# -> MaCave
# -> languages
# -> bottles

pl = plary.join “/”

print "pl = ", pl, “\n” # -> pl =
/com-ultrid-yvonthor/MaCave/languages/bottles

i expected com-ultrid-yvonthor/MaCave/languages/bottles instead (without
first “/”)

i don’t catch out my mistake…

the same arroses with :

plary = [context.split(fs).compact, path.split(fs).compact, “languages”,
“bottles”]

···


yt

use File.join() and you’r life will be beter :slight_smile:

···

il Wed, 31 Mar 2004 10:26:23 +0200, yvon.thoravalNO-SPAM@free.fr (Yvon Thoraval) ha scritto::

i get a very small prob with join :

(fs stands for file.separator)

[Yvon Thoraval yvon.thoravalNO-SPAM@free.fr, 2004-03-31 10.29 CEST]

i get a very small prob with join :

(fs stands for file.separator)

plary = Array.new
plary << context.split(fs).compact << path.split(fs).compact <<
“languages” << “bottles”

You realize that the first 2 elements of plary are arrays, yes?

print "size = ", plary.size, “\n” # → size = 4

plary.each {|p| print p, “\n” } # → com-ultrid-yvonthor
# → MaCave
# → languages
# → bottles

pl = plary.join “/”

print "pl = ", pl, “\n” # → pl =
/com-ultrid-yvonthor/MaCave/languages/bottles

i expected com-ultrid-yvonthor/MaCave/languages/bottles instead (without
first “/”)

I suppose, the variable ‘context’ was “/com-ultrid-yvonthor”. Your
array plary is [[“”, “com-ultrid-ivonthor”], [“MaCave”], “languages”,
“bottles”].

#join seems to flatten the array before joining. So, the first element
is the empty string.

#compact only removes nils, not empty strings. You should try another
way to remove empty strings. Probably using #reject or #delete_if.

#<< doesn’t concatenate arrays; it only appends its argument to the
end of the array. To concatenate you can use #+ or #concat.

Good luck.

“gabriele renzi” surrender_it@remove.yahoo.it schrieb im Newsbeitrag
news:pi3l6056a0plvp0q1mprgl5akrdau76371@4ax.com

i get a very small prob with join :

(fs stands for file.separator)

use File.join() and you’r life will be beter :slight_smile:

… together wit File.split():

dir, base = File.split fileName
complete = File.join dir, base

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.

Regards

robert
···

il Wed, 31 Mar 2004 10:26:23 +0200, yvon.thoravalNO-SPAM@free.fr (Yvon > Thoraval) ha scritto::

ok tanxs, i’ve changed to :

context = $tag.getPropertyInTree(“context”) # = “/com-ultrid-yvonthor/”
path = $tag.getPropertyInTree(“path”) # = “MaCave/”
fs = JavaLang::System.getProperty(“file.separator”) # = “/” (MacOS X)

contextL = context.split(fs)
contextL.delete(“”)
pathL = path.split(fs)
pathL.delete(“”)

plary = Array.new
plary << contextL << pathL << “languages” << $tbl # $tbl =
“t_bottles_btl”
pl = plary.join “/”

···

Carlos angus@quovadis.com.ar wrote:

#compact only removes nils, not empty strings. You should try another
way to remove empty strings. Probably using #reject or #delete_if.

#<< doesn’t concatenate arrays; it only appends its argument to the
end of the array. To concatenate you can use #+ or #concat.


yt

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], i’ve changed
#compact to #delete(“”) and works fine

···

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.


yt

“Yvon Thoraval” yvon.thoravalNO-SPAM@free.fr schrieb im Newsbeitrag
news:1gbijxj.jnr3jyxfhxp6N%yvon.thoravalNO-SPAM@free.fr…

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

Robert Klemme bob.news@gmx.net wrote:

File.split doesn’t apply here, IMHO, because i do have “hard coded” path
parts (ie are attributes of an xml tag), the file separator is always
“/”, doesn’t depend upon platform.

then, i do right now :

pl = File.join(( context + path + “languages/bottles”).split(“/”).select
{|x| x && x != “”})

and i’m sure Ruby is aware of File::SEPARATOR, no more need to read the
java one…which is the same.

tanxs for your help !

···

Robert Klemme bob.news@gmx.net wrote:

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.


yt