Confusion about Dir [] and File.join () in Ruby

I come across a simple program about Dir[]and File.join()in Ruby,

blobs_dir = '/path/to/dir'
Dir[File.join(blobs_dir, "**", "*")].each do |file|
       FileUtils.rm_rf(file) if File.symlink?(file)

I have two confusions:

First, what do the second and third parameters mean in File.join(@blobs_dir, "**", "*")?

Secondly, what is the use Dir[]in Ruby? I know this is equivalent Dir.glob(), however I don't know if it really is Dir.glob().

+3
source share
4 answers

File.join () simply combines all its arguments with a single slash. For instance,

File.join("a", "b", "c")

returns "a / b / c". This is almost equivalent to the more commonly used Array union method, like this:

["hello", "ruby", "world"].join(", ")
# => "hello, ruby, world"

File.join(), , : , -, , "/" ( "," ). Ruby - , , .

Dir [] , , "*" "**" . ,

Dir["/var/*"]
# => ["/var/lock", "/var/backups", "/var/lib", "/var/tmp", "/var/opt", "/var/local", "/var/run", "/var/spool", "/var/log", "/var/cache", "/var/mail"]

Dir["/var/**/*"]
# => ["/var/lock", "/var/backups", "/var/backups/dpkg.status.3.gz", "/var/backups/passwd.bak" ... (all files in all dirs in '/var')]

+3
File.join(blobs_dir, "**", "*")

glob. /path/to/dir/**/*

** * :

*:
**:

, /path/to/dir.

+4

File::join File::SEPARATOR ( /):

File.join('a', 'b', 'c')
# => "a/b/c"

Dir::glob , .

This template /path/to/dir/**/*corresponds to a recursive file (below /path/to/dir).

+3
source

From here :

glob -- Expands pattern, which is an Array of patterns or a pattern String, and returns the results as matches or as arguments given to the block.
*    -- Matches any file
**   -- Matches directories recursively
+1
source

All Articles