File.lib bug?

Hello!

Using 1.6.7 on a win98 box:

C:>irb
irb(main):001:0> d=File.join(‘c:/’,‘windows’)
"c://windows"
irb(main):002:0> File.directory?(d)
false
irb(main):003:0> File.directory?(‘c:/windows’)
true
irb(main):004:0> File.expand_path(d)
"c:/windows"
irb(main):005:0> File.directory?(File.expand_path(d))
true

Has this been fixed in 1.7.x? I’ve found the expand_path() trick in ruby-
talk, but couldn’t find any other reference to this problem.

BTW: is there some documentation about 1.7.x? I’ve searched for it, but
wasn’t able to find anything…

Thank you, Andrea

Using 1.6.7 on a win98 box:

C:>irb
irb(main):001:0> d=File.join(‘c:/’,‘windows’)
“c://windows”
irb(main):002:0> File.directory?(d)
false
irb(main):003:0> File.directory?(‘c:/windows’)
true
irb(main):004:0> File.expand_path(d)
“c:/windows”
irb(main):005:0> File.directory?(File.expand_path(d))
true

Has this been fixed in 1.7.x? I’ve found the expand_path() trick in ruby-
talk, but couldn’t find any other reference to this problem.

I’m guessing that the reason why File.directory?(d) == false was
File.exist?(d) == false.

File.join is intended to absorb the deference of path separator among
platforms. File.join(a,b,c,d) is equivalent to

[a,b,c,d].join(File::SEPARATOR)

So, File.join(‘c:/’, ‘windows’) should be File.join(‘c:’, ‘windows’).

BTW: is there some documentation about 1.7.x? I’ve searched for it, but
wasn’t able to find anything…

I’m afraid documentation about 1.7 is written in only Japanese. That
is being written on an Wiki system.

– Gotoken

···

At Mon, 23 Sep 2002 21:56:26 +0900, Overnight wrote:

C:>irb
irb(main):001:0> d=File.join(‘c:/’,‘windows’)
“c://windows”
irb(main):002:0> File.directory?(d)
false
irb(main):003:0> File.directory?(‘c:/windows’)
true
irb(main):004:0> File.expand_path(d)
“c:/windows”
irb(main):005:0> File.directory?(File.expand_path(d))
true
I’m guessing that the reason why File.directory?(d) == false was
File.exist?(d) == false.
File.join is intended to absorb the deference of path separator among
platforms. File.join(a,b,c,d) is equivalent to
[a,b,c,d].join(File::SEPARATOR)
So, File.join(‘c:/’, ‘windows’) should be File.join(‘c:’, ‘windows’).

Hmmmm, imho i’d like to see this fixed… Under Unix, if I’m not wrong,
something like ‘/usr//lib’ won’t hurt, but under Windows it will, as I
showed in my example (btw, it looks like the presence of double slash in
the argument to File.directory? makes Ruby/Windows go and look for some
remote hosts, considering the long delay it takes before it returns
false…)

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 ''…

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).

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…

I’m afraid documentation about 1.7 is written in only Japanese. That
is being written on an Wiki system.

I’ll have to wait, not ready for a Japanese quick course at the moment :-
)

Thank you for your help,

Andrea

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.

···

At Tue, 24 Sep 2002 05:19:14 +0900, Overnight wrote:


Nobu Nakada

Hello,

In message “Re: File.lib bug?”

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.

There is summary of the discussion:
http://www.ruby-lang.org/ja/man-1.6/?cmd=view;name=DOSISH+�б�

Sorry, this summary is Japanese.
But I hope you can read code sample…

Samples with ruby-list' and ruby-dev’ are suggestion.
Other samples are already implemented.

Regards.

···

on Sep.24,2002 13:49:36, nobu.nokada@softhome.net wrote:

At Tue, 24 Sep 2002 05:19:14 +0900, > Overnight wrote:

U.Nakamura usa@osb.att.ne.jp

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(/[1]+|[\/]+$/,‘’).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:


  1. \/ ↩︎