新手问题 /lib、/lib/**/ 两个路径有什么差别

artone · 发布于 2012年11月03日 · 最后由 xds2000 回复于 2012年11月03日 · 1890 次阅读
3417

请问这两个路径有什么差别?** 两个星号又代表什么意思?

config.autoload_paths += Dir["#{config.root}/lib", "#{config.root}/lib/**/"]

一个星号 * 通常表示通配符,不明白两个星号 ** 是什么意思。

共收到 7 条回复
1986
jyz19880823 · #1 · 2012年11月03日

莫非表示子目录?

2973
small_fish__ · #2 · 2012年11月03日

应该是递归下面所有目录文件

121
lyfi2003 · #3 · 2012年11月03日

#1楼 @jyz19880823 #2楼 @small_fish__ ** 所有子目录。包括子目录的子目录。 * 只是一级子目录。

3417
artone · #4 · 2012年11月03日

#3楼 @lyfi2003 想请问一下,所以在这里,"#{config.root}/lib" 是多馀的吗?

8
hooopo · #5 · 2012年11月03日 1 个赞

都不看文档哇....... http://ruby-doc.org/core-1.9.3/Dir.html#method-c-glob

glob( pattern, [flags] ) {| filename | block } → nil

Returns the filenames found by expanding pattern which is an Array of the patterns or the pattern String, either as an array or as parameters to the block. Note that this pattern is not a regexp (it’s closer to a shell glob). See File::fnmatch for the meaning of the flags parameter. Note that case sensitivity depends on your system (so File::FNM_CASEFOLD is ignored), as does the order in which the results are returned.

*

Matches any file. Can be restricted by other values in the glob. * will match all files; c* will match all files beginning with c; c will match all files ending with c; and *c will match all files that have c in them (including at the beginning or end). Equivalent to / .* /x in regexp. Note, this will not match Unix-like hidden files (dotfiles). In order to include those in the match results, you must use something like “{,.}”.

**

Matches directories recursively.

?

Matches any one character. Equivalent to /.{1}/ in regexp.

[set]

Matches any one character in set. Behaves exactly like character sets in Regexp, including set negation ([a-z]).

{p,q}

Matches either literal p or literal q. Matching literals may be more than one character in length. More than two literals may be specified. Equivalent to pattern alternation in regexp.

\

Escapes the next metacharacter. Note that this means you cannot use backslash in windows as part of a glob, i.e. Dir will not work use Dir instead

Dir["config.?"]                     #=> ["config.h"]
Dir.glob("config.?")                #=> ["config.h"]
Dir.glob("*.[a-z][a-z]")            #=> ["main.rb"]
Dir.glob("*.[^r]*")                 #=> ["config.h"]
Dir.glob("*.{rb,h}")                #=> ["main.rb", "config.h"]
Dir.glob("*")                       #=> ["config.h", "main.rb"]
Dir.glob("*", File::FNM_DOTMATCH)   #=> [".", "..", "config.h", "main.rb"]

rbfiles = File.join("**", "*.rb")
Dir.glob(rbfiles)                   #=> ["main.rb",
                                    #    "lib/song.rb",
                                    #    "lib/song/karaoke.rb"]
libdirs = File.join("**", "lib")
Dir.glob(libdirs)                   #=> ["lib"]

librbfiles = File.join("**", "lib", "**", "*.rb")
Dir.glob(librbfiles)                #=> ["lib/song.rb",
                                    #    "lib/song/karaoke.rb"]

librbfiles = File.join("**", "lib", "*.rb")
Dir.glob(librbfiles)                #=> ["lib/song.rb"]
121
lyfi2003 · #6 · 2012年11月03日

#4楼 @artone 可以这样说。是已经包含了。

202
xds2000 · #7 · 2012年11月03日

@hooopo 还真没看过,学习。

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册