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"
=> "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.
···
On 3月30日, 上午9时21分, "Bret Pettichord" <bpettich...@gmail.com> 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"
=> "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
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"
=> "foo/bar"
irb(main):002:0>
I told my collegues that instead of using File.join, they should just
use + "\\" +
I'd probably rather use ["foo", "bar"].join File::SEPARATOR (if that's set to \\ on Windows).
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).
Exactly. File.join produces paths that Ruby can correctly use. That's the rationale.
Is there a rationale? Is there a different library that i should be
using instead?
Well, you could do path.tr '/', '\\' before passing the path to some other process.
Kind regards
robert
···
On 30.03.2007 03:21, Bret Pettichord 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"
=> "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 Unixes):
class File
def to_s expand=false
(expand ? File.expand_path(path) : path).gsub('/', SEPARATOR)
end
end
Used then like this:
>> File.new('/tmp/foo','w').to_s(true)
=> "/tmp/foo"
>> File.new('/tmp/../tmp/./foo','w').to_s(true)
=> "/tmp/foo"
>> File.new('/tmp/../tmp/./foo','w').to_s
=> "/tmp/../tmp/./foo"
Or presumably on Windows:
>> File.new(File.join('foo','bar'),'w').to_s(true)
=> "C:\\Users\\bret\\foo\\bar"
>> File.new(File.join('foo','bar'),'w').to_s
=> "foo\\bar"
You could also define a String#as_file to do the same .gsub('/', ::File::SEPARATOR)
If Microsoft hadn't broken the path separator in the first place, they wouldn't have had to fix it.
-Rob
Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
···
On Mar 29, 2007, at 10:35 PM, Wang Dong wrote:
On 3月30日, 上午9时21分, "Bret Pettichord" <bpettich...@gmail.com> > wrote:
File::SEPARATOR is set to '/' on all platforms:
C:\>ruby -v
ruby 1.8.5 (2006-08-25) [i386-mswin32]
C:\>irb
irb(main):001:0> File::SEPARATOR
=> "/"
irb(main):002:0> File::Separator
=> "/"
Best regards,
Wayne
···
On 3/30/07, Robert Klemme <shortcutter@googlemail.com> wrote:
I'd probably rather use ["foo", "bar"].join File::SEPARATOR (if that's
set to \\ on Windows).
---
Wayne Vucenic
No Bugs Software
Ruby, Erlang and C# Agile Contract Programming in Silicon Valley
Wuby always uses backslashes.
Regards,
Dan
···
On Mar 30, 3:07 pm, Robert Klemme <shortcut...@googlemail.com> wrote:
On 30.03.2007 03:21, Bret Pettichord 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"
> => "foo/bar"
> irb(main):002:0>
> I told my collegues that instead of using File.join, they should just
> use + "\\" +
I'd probably rather use ["foo", "bar"].join File::SEPARATOR (if that's
set to \\ on Windows).
\T\h\e\n\ \\\/\\\/\o\u\l\d\n\'\t it be \\\/\\\/\u\b\y <G>
···
On 4/2/07, Daniel Berger <djberg96@gmail.com> wrote:
Wuby always uses backslashes.
--
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/