Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Dir.glob and Find

Tags:

ruby

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.

like image 742
Balzard Avatar asked Mar 08 '14 10:03

Balzard


People also ask

What does DIR glob do?

"Globbing" files (with Dir. glob) in Ruby allows you to select just the files you want, such as all the XML files, in a given directory.

How do I get the current directory in Ruby?

pwd : To check the current working directory, pwd(present working directory) method is used. 5. chdir : To change the current working directory, chdir method is used.


1 Answers

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.

like image 86
sawa Avatar answered Oct 01 '22 01:10

sawa