如何检查 Ruby 中给定的目录是否存在

发布于 2024-07-26 08:42:16 字数 239 浏览 2 评论 0原文

我正在尝试编写一个脚本,根据指定目录是否存在自动检查或更新 Subversion URL。

由于某种原因,我的代码无法正常工作,并且始终返回 true,即使它是 false:

def directory_exists?(directory)
  return false if Dir[directory] == nil
  true
end

我做错了什么?

I am trying to write a script which automatically checks out or updates a Subversion URL based on whether a specified directory exists or not.

For some reason, my code isn't working and always returns true even if it's false:

def directory_exists?(directory)
  return false if Dir[directory] == nil
  true
end

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

江城子 2024-08-02 08:42:16

如果您要查找的文件是否是目录而不仅仅是文件很重要,则可以使用 File.directory?Dir.exist?。 仅当文件存在并且是目录时才会返回 true。

顺便说一句,编写该方法的更惯用的方法是利用 Ruby 自动返回方法内最后一个表达式的结果这一事实。 因此,您可以这样写:

def directory_exists?(directory)
  File.directory?(directory)
end

请注意,在本例中不需要使用方法。

If it matters whether the file you're looking for is a directory and not just a file, you could use File.directory? or Dir.exist?. This will return true only if the file exists and is a directory.

As an aside, a more idiomatic way to write the method would be to take advantage of the fact that Ruby automatically returns the result of the last expression inside the method. Thus, you could write it like this:

def directory_exists?(directory)
  File.directory?(directory)
end

Note that using a method is not necessary in the present case.

清君侧 2024-08-02 08:42:16

您还可以使用 Dir::exist ? 像这样:

Dir.exist?('Directory Name')

如果“目录名称”是目录,则返回 true,否则返回 false1

You can also use Dir::exist? like so:

Dir.exist?('Directory Name')

Returns true if the 'Directory Name' is a directory, false otherwise.1

情绪 2024-08-02 08:42:16

所有其他答案都是正确的,但是,如果您尝试检查用户主目录中的目录,则可能会遇到问题。 确保在检查之前展开相对路径:

File.exists? '~/exists'
=> false
File.directory? '~/exists'
=> false
File.exists? File.expand_path('~/exists')
=> true

All the other answers are correct, however, you might have problems if you're trying to check directory in a user's home directory. Make sure you expand the relative path before checking:

File.exists? '~/exists'
=> false
File.directory? '~/exists'
=> false
File.exists? File.expand_path('~/exists')
=> true
一绘本一梦想 2024-08-02 08:42:16
File.exist?("directory")

Dir[] 返回一个数组,因此它永远不会是 nil。 如果您想按照自己的方式进行操作,则可以执行以下

Dir["directory"].empty?

操作:如果未找到,则返回 true

File.exist?("directory")

Dir[] returns an array, so it will never be nil. If you want to do it your way, you could do

Dir["directory"].empty?

which will return true if it wasn't found.

愛放△進行李 2024-08-02 08:42:16

您可以使用 Kernel#test< /code>

test ?d, 'some directory'

它的起源来自 https://ss64.com/bash/test .html
你会注意到 bash test 有这个标志 -d 来测试目录是否存在
-d file 如果文件是目录则为真。 [[ -d 演示文件 ]]

You could use Kernel#test:

test ?d, 'some directory'

it gets it's origins from https://ss64.com/bash/test.html
you will notice bash test has this flag -d to test if a directory exists
-d file True if file is a Directory. [[ -d demofile ]]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文