In article 200209240449.g8O4nY027679@sharui.nakada.kanuma.tochigi.jp,
nobu.nokada@softhome.net says…
Hi,
Using File.join(‘c:’,‘windows’) will work of course, but under Windows I
never refer to the root of a disk as C:, D:, etc, always as C:, D:.
This is an habit imposed from the shell, which interprets ‘C:’ (alone,
with no slash) as “current working dir of disk C”. So ‘C:myfile’ is the
same as ‘C:\myfile’ only if the working dir of C: is ''…
Essentially, the specification about drive letter is too
ambiguous. In ruby-dev ML, we had a discussion about the
behavior of File.split, File.join and so on, but I don’t
remember the detail now.
Last observation: with 1.6.7, File::SEPARATOR evals to ‘/’, and this
looks definitely wrong to me (even if Ruby understands both ‘/’ and ''
in paths).
And also Windows does.
It’s wonderful to see how easily such things can be fixed in Ruby, but
I’d like to see them fixed officially: I guess I’m not the only one who
got bitten by this…
A method such as File.extern_path converts path would be useful
to pass it to other programs.
Hello Nobu.
I have written the code below (consider it as my homework :-), not fully
tested ), which should redefine most standard methods involving
filename(s), converting them in a standard format (‘/’ as a separator, no
multiple '/'s, no trailing ‘/’)
It seems to work:
irb(main):016:0> FileTest._oldexists?(‘c://windows//win.ini’)
false
irb(main):017:0> FileTest.exists?(‘c://windows//win.ini’)
true
and so on. However:
irb(main):018:0> File.exists?(‘c://windows//win.ini’)
false
A new problem here, which I couldn’t solve. Looks like the FileTest’s
proxy class in File refers to _oldexists?() rather then exists?() How
could I solve this?
Another question: is there way to stop “load/require” of a file. The file
below is something like:
if RUBY_PLATFORM =~ /mswin32/
...code...
end
Is there a way to write something like the following?
stop_load unless RUBY_PLATFORM =~ /mswin32/
...code...
As a beginner, every comment on the code will be very appreciated!
Thanks!
Andrea
<<<fixes.rb>>>
if RUBY_PLATFORM =~ /mswin32/
class Module
private
#redefine given methods to use a _fix_path()-ed first argument
#see Object#_fix_path
def fixwinpath(*names)
#redefine f(a,...) as f(_fix_path(a),...) for each f in
names
names.each {|n|
oldn= n==‘’ ? ‘_oldbrackets’ : ‘_old’+n.to_s
alias_method(oldn,n.to_s)
module_eval(“def #{n.to_s}(arg,*rest,&block);
#{oldn}(_fix_path(arg),*rest,&block); end”)
}
end
end
class Object
private
def _fix_path(path)
path.gsub(/[\\\/]+/,'/').gsub(/\/+$/,'')
end
public
alias :_oldAutoload :autoload
def autoload(arg,file)
_oldAutoload(arg,_fix_path(file))
end
alias :_oldTest :test
def test(arg,*files)
_oldTest(arg,*(files.collect {|f| _fix_path(f)}))
end
fixwinpath :load,:open,:require
end
class File
class <<self
alias :_oldChmod :chmod
def chmod(arg,*files)
_oldChmod(arg,files.collect {|f| _fix_path(f)})
end
alias :_oldChown :chown
def chown(arg1,arg2,*files)
_oldChown(arg1,arg2,files.collect {|f|
_fix_path(f)})
end
alias :_oldDelete :delete
def delete(*files)
_oldDelete(files.collect {|f| _fix_path(f)})
end
alias :_oldJoin :join
def join(*args)
_oldJoin(args.collect {|a|
a.gsub(/+|[\/]+$/,‘’).gsub(/[\/]+/,‘/’)})
end
alias :_oldRename :rename
def rename(arg1,arg2)
_oldRename(_fix_path(arg1),_fix_path(arg2))
end
alias :_oldUnlink :unlink
def unlink(*files)
_oldUnlink(files.collect {|f| _fix_path(f)})
end
alias :_oldUtime :utime
def utime(arg1,arg2,*files)
_oldUtime(arg1,arg2,files.collect {|f|
_fix_path(f)})
end
fixwinpath :atime,:basename,:ctime,:dirname,:ftype, \
:lstat,:mtime,:new,:open,:readlink,:size,:split,\
:stat,:truncate
#new method
def extern_path(path)
path.gsub(/[\\\/]+/,'\\').gsub(/\\+$/,'')
end
end
end
class Dir
class <<self
fixwinpath *(public_instance_methods-[‘pwd’,‘getwd’])
end
end
module FileTest
class <<self
fixwinpath *public_instance_methods
end
end
end
···
At Tue, 24 Sep 2002 05:19:14 +0900, > Overnight wrote: