Misc #16396


What is the reason for this behaviour of Find.find?

Added by stiuna (Juan Gregorio) almost 3 years ago. Updated almost 3 years ago.



When I have a script at D:\Downloads\Ruby 2.5.3\rbL\comp\codeShort.rb with few other files in the same folder, the following searches only the folder where the .rb script is located:


When I have a script at D:\Downloads\Ruby 2.5.3\rbL\codeShort.rb with many other files in the same folder, the same code as above searches the entire disk D.

To search the entire disk D in the first case, I did this:


But I don't understand why the two cases behave differently with the same instruction just because they script are in different directories.

Updated by shevegen (Robert A. Heiler) almost 3 years ago

I can not say whether this is the case or not (I work only on Linux), also due to me
using Dir[] preferentially instead. With Dir[] I always make sure to have a trailing
'/' token if I wish to work with directories, and subdirectories, respectively, e.g:

pp Dir['/tmp/**/**/']

Based on that I tend to then use .select or .reject, such as when I wish to find
only certain files. There is also Dir.glob() but I sort of transitioned into Dir[]
completely - it just seems so much simpler to me than all alternatives.

Back to your comment - it may be best to compare the results that you have found
with Find.find(), to Dir[], and look whether there are any alternatives that may
be a bug. If it is a bug, it could help the ruby core team if you could reproduce
it into a small use case that they can test (I think most ruby core team members
use linux or mac, although perhaps some also have windows or some dual-boot setup.)

The easier it can be made for the core team to test, the better if the goal is to
resolve problems (if they are really problems that is).

On a side note - I have found that my ruby code works fine on windows too even if
I just use "/" in file paths. The reason I adopted trailing / a long time ago is
because it made it simpler to me, understanding what is going on. Perhaps your
description may also be an example where trailing / are bettr to use, as in them
being more explicit - even if there should not be a difference (I really can't
say, I don't use or seem to need Find.find() really).

Updated by stiuna (Juan Gregorio) almost 3 years ago

pp Dir['/tmp/**/**/']

Hello, I loved your alternative, from now on I'll use Dir.

Regarding the "bug" of Find I think I can prove it one of these days I report it.

Actions #3

Updated by sawa (Tsuyoshi Sawada) almost 3 years ago

  • Description updated (diff)
Actions #4

Updated by sawa (Tsuyoshi Sawada) almost 3 years ago

  • Description updated (diff)

Updated by Dan0042 (Daniel DeLorme) almost 3 years ago

From what I remember of Windows, Find.find('D:') would search in the current directory of D:
My guess is that you're actually executing the first script from D:\Downloads\Ruby 2.5.3\rbL\comp\ and the second script from D:\ (or maybe C:\some\dir\?)
So you may want to check the value of Dir.pwd in each case.


Also available in: Atom PDF