简体   繁体   English

项目外部的路径,Ruby Dir

[英]Path outside of project, Ruby Dir

I'm trying to return a list of files for a directory using the following code: 我正在尝试使用以下代码返回目录的文件列表:

Dir[directory]

The issue I'm having is this appears to always take the url as relative to the project location. 我遇到的问题是,此问题似乎总是以相对于项目位置的网址进行。 For example passing '*' into Dir returns an array containing my Gemfile etc.. I want to be able to get a list of files for directories such as /Users/jason/Pictures however when i pass in /Users/jason/Pictures/* I am returned an empty array. 例如,将“ *”传递到Dir会返回一个包含我的Gemfile等的数组。我希望能够获取目录(例如/Users/jason/Pictures的文件列表,但是当我传递/Users/jason/Pictures/*我返回了一个空数组。

Any pointers would be greatly appreciated. 任何指针将不胜感激。 Thanks. 谢谢。

Try: 尝试:

Dir['/Users/jason/Pictures/*']

If there are files or directories embedded in that path you'll get an array back from Dir[] . 如果该路径中嵌入了文件或目录,则将从Dir[]返回数组。 If you don't, and you're sure there are files in the directory, then there's something wrong, but it's most likely in the glob string you gave it. 如果不这样做,并且您确定目录中有文件,则可能有问题,但这很可能是在您给它提供的glob字符串中。 Confirm it's the right path by moving to it and using pwd to see what the OS thinks it is. 移至该路径并使用pwd查看操作系统的想法,以确认这是正确的路径。 Dir[] doesn't return an error if the glob string isn't valid, it just returns an empty array. 如果全局字符串无效, Dir[]不会返回错误,它只会返回一个空数组。

You could try using File.exist?('glob string without *') and see whether that path exists before trying to iterate it. 您可以尝试使用File.exist?('glob string without *') ,然后在尝试进行迭代之前查看该路径是否存在。

On my machine, running this in IRB in my home directory: 在我的机器上,在主目录的IRB中运行此命令:

Dir['./vim/bundle/*']

Returns: 返回值:

[
    [ 0] "./vim/bundle/closetag.vim",
    [ 1] "./vim/bundle/ctrlp.vim",
    ...
    [31] "./vim/bundle/vim-vividchalk",
    [32] "./vim/bundle/Vundle.vim"
]

I can use an absolute path also: 我也可以使用绝对路径:

Dir['/Users/ttm/vim/bundle/*']

And get: 得到:

[
    [ 0] "/Users/ttm/vim/bundle/closetag.vim",
    [ 1] "/Users/ttm/vim/bundle/ctrlp.vim",
    ...
    [31] "/Users/ttm/vim/bundle/vim-vividchalk",
    [32] "/Users/ttm/vim/bundle/Vundle.vim"
]

Notice that you get relative pathnames if you use a relative glob string, and absolute pathnames for an absolute glob string. 请注意,如果使用相对的全局字符串,则会得到相对路径名,而绝对的全局字符串将得到绝对路径名。

You need to use the entries method 您需要使用entry方法

Dir.entries('/')
=> [".", "..", ".apdisk", ".com.apple.backupd.mvlist.plist", ".dbfseventsd", ".DocumentRevisions-V100", ".DS_Store", ".file", ".fseventsd", ".hotfiles.btree", ".MobileBackups",".OSInstallMessages", ".PKInstallSandboxManager", ".Spotlight-V100", ".SymAVx86QSFile", ".Trashes", ".vol", "Applications", "bin", "cores", "dev", "etc", "home", "Library", "net", "Network", "opt", "private", "sbin", "System", "tmp", "Users", "usr", "var", "Volumes", "~"]

Here is the oficial documentation http://www.ruby-doc.org/core-2.1.2/Dir.html#method-c-entries 这是官方文档http://www.ruby-doc.org/core-2.1.2/Dir.html#method-c-entries

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM