Arrays Ruby-如何读取以模式开头的所有符号链接

Arrays Ruby-如何读取以模式开头的所有符号链接,arrays,ruby,symlink,Arrays,Ruby,Symlink,在ruby中,如何读取路径上的所有符号链接并将它们放入数组中- 例如,我在路径/垃圾箱中有以下符号链接- current_instance1 -> ABC current_instance2 -> DEF current_instance3 -> GHI 如果您只想列出所有符号链接,我想读取以“current_u2;”开头的所有符号链接,并将它们填充到一个数组中: Dir.glob("/path/to/dir/*").find_all { |file| File.symlin

在ruby中,如何读取路径上的所有符号链接并将它们放入数组中- 例如,我在路径/垃圾箱中有以下符号链接-

current_instance1 -> ABC
current_instance2 -> DEF
current_instance3 -> GHI

如果您只想列出所有符号链接,我想读取以“current_u2;”开头的所有符号链接,并将它们填充到一个数组中

Dir.glob("/path/to/dir/*").find_all { |file| File.symlink?(file) }
如果要列出带有其目标的符号链接:

Dir.glob("/path/to/dir/*").map { |file|  {file => File.readlink(file)} if File.symlink?(file) }.compact

如果只想列出所有符号链接:

Dir.glob("/path/to/dir/*").find_all { |file| File.symlink?(file) }
如果要列出带有其目标的符号链接:

Dir.glob("/path/to/dir/*").map { |file|  {file => File.readlink(file)} if File.symlink?(file) }.compact
Dir.glob(“//current|*”).map{| file | file.readlink(file)if file.symlink?(file)}.compact
说明:

Dir.glob("/<path_to_dir>/current_*") # Matches all files beginning with current_ in the specified path

Dir.glob("/<path_to_dir>/*current_") # Matches all files ending with current_ in the specified path

Dir.glob("/<path_to_dir>/*current_*") # Match all files that have c in them (including at the beginning or end) in the specified path

.map { |file| # iterates and pushes result into array

File.readlink(file) if File.symlink?(file) # Get the target file if the file is a symlink

}.compact # if the file is not a symlink in the specified path, map fills nil by default. Using compact at the end removes all nil entries from the resulting array and gives you only symlink target files in an array.
Dir.glob(“//current_*”)匹配指定路径中以current开头的所有文件
Dir.glob(“//*current”)匹配指定路径中以current结尾的所有文件
Dir.glob(“//*current_*”)#匹配指定路径中包含c的所有文件(包括开头或结尾)
.map{| file |#迭代并将结果推送到数组中
File.readlink(File)if File.symlink?(File)#如果文件是symlink,则获取目标文件
}.compact#如果文件不是指定路径中的符号链接,则默认情况下,map填充nil。最后使用compact将从生成的数组中删除所有nil项,并只提供数组中的符号链接目标文件。
更新: OP只需要文件名,我们应该使用
File.basename(File.readlink(File))

Dir.glob(“//current.*”).map{File | File.readlink(File)if File.symlink?(File)}.compact
说明:

Dir.glob("/<path_to_dir>/current_*") # Matches all files beginning with current_ in the specified path

Dir.glob("/<path_to_dir>/*current_") # Matches all files ending with current_ in the specified path

Dir.glob("/<path_to_dir>/*current_*") # Match all files that have c in them (including at the beginning or end) in the specified path

.map { |file| # iterates and pushes result into array

File.readlink(file) if File.symlink?(file) # Get the target file if the file is a symlink

}.compact # if the file is not a symlink in the specified path, map fills nil by default. Using compact at the end removes all nil entries from the resulting array and gives you only symlink target files in an array.
Dir.glob(“//current_*”)匹配指定路径中以current开头的所有文件
Dir.glob(“//*current”)匹配指定路径中以current结尾的所有文件
Dir.glob(“//*current_*”)#匹配指定路径中包含c的所有文件(包括开头或结尾)
.map{| file |#迭代并将结果推送到数组中
File.readlink(File)if File.symlink?(File)#如果文件是symlink,则获取目标文件
}.compact#如果文件不是指定路径中的符号链接,则默认情况下,map填充nil。最后使用compact将从生成的数组中删除所有nil项,并只提供数组中的符号链接目标文件。
更新:
OP只需要文件名,我们应该使用
File.basename(File.readlink(File))

你能告诉我你要找的输出吗?我的数组应该是myarray=[“ABC”,“DEF”,“GHI”]你能告诉我你要找的输出吗?我的数组应该是myarray=[“ABC”,“DEF”,“GHI”]谢谢Vamsi,但它返回整个路径以及目录名-[“/ABC”、“/DEF”、“/GHI”],而我只需要[“ABC”、“DEF”、“GHI”]。我该怎么做呢?谢谢Vamsi,但它会返回整个路径以及目录名-[“/ABC”、“/DEF”、“/GHI”],而我只需要[“ABC”、“DEF”、“GHI”]。我该怎么做?