7

What is the difference between:

Dir.glob(File.join('.', '**', '*')).each do |e|

end

and

require 'find'
Find.find('.').each do |e|

end

Despite the fact that Find is 3 times slower.

1 Answer 1

1

Dir.glob iterates over all files/directories that match the argument possibly including wild characters in a flat way, whereas Find.find first iterates over a direct files/directories that match the exact path name, then looks into its direct files/directories in a recursive way. With Find.find, you can skip the whole directory.

2
  • Thanks! What you mean is that glob is doing a depth first traversal while Find is doing a breadth first traversal? Right?
    – Balzard
    Mar 9, 2014 at 12:26
  • No. glob dispenses with the notion of depth. I haven't made it clear, but find actually does depth first.
    – sawa
    Mar 9, 2014 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.