On Mar 29, 2007, at 10:35 PM, Wang Dong wrote:
> On 3=D4=C230=C8=D5, =C9=CF=CE=E79=CA=B121=B7=D6, "Bret Pettichord" =
<bpettich...@gmail.com> =20
> wrote:
>> I just had to work on some code that ran into trouble, because Ruby
>> uses "/" to separate folders, but the path was being passed to a
>> separate command (using system) that expected Window's paths to use
>> "\".
>>
>> C:\Users\bret>irb
>> irb(main):001:0> File.join "foo", "bar"
>> =3D> "foo/bar"
>> irb(main):002:0>
>>
>> I told my collegues that instead of using File.join, they should just
>> use + "\\" +
>> But this got me to wondering what the point of File.join was if you
>> couldn't trust it to construct paths correctly (unless you were sure
>> that the path was only going to be used by other ruby commands).
>>
>> Is there a rationale? Is there a different library that i should be
>> using instead?
>>
>> bret
>
> Windows NT support '/' but not win98 or DOS.
You could try something like this (note UNTESTED since I'm only on =20
Unixes):
class File
def to_s expand=3Dfalse
(expand ? File.expand_path(path) : path).gsub('/', SEPARATOR)
end
end
Used then like this:
>> File.new('/tmp/foo','w').to_s(true)
=3D> "/tmp/foo"
>> File.new('/tmp/../tmp/./foo','w').to_s(true)
=3D> "/tmp/foo"
>> File.new('/tmp/../tmp/./foo','w').to_s
=3D> "/tmp/../tmp/./foo"
Or presumably on Windows:
>> File.new(File.join('foo','bar'),'w').to_s(true)
=3D> "C:\\Users\\bret\\foo\\bar"
>> File.new(File.join('foo','bar'),'w').to_s
=3D> "foo\\bar"
You could also define a String#as_file to do the same .gsub=20
('/', ::File::SEPARATOR)
If Microsoft hadn't broken the path separator in the first place, =20
they wouldn't have had to fix it.
-Rob
Rob Biedenharn
http://agileconsultingllc.com