扫描子目录然后切换到该目录?

时间:2022-09-12 23:57:28

I have a large program in Ruby that's being distributed over several projects. The only problem is the other several projects all have separate members on them that have their own way of coding, and the program is set up to be alongside a specific path, which was for the first project that I used it w/. I've had so many errors to correct that were simply mistaken paths. What I want to do now is scan an entire project for an individual directory (as the program's overhead directory is constant in every instance of its usage) and then set the path to that directory. To keep things simplistic, let's say it's w/ a Rails project, so Rails.root can be the overhead, and the directory to search for is myawesomedir. Any help is greatly appreciated!

我在Ruby中有一个大型程序,它分布在几个项目中。唯一的问题是其他几个项目都有独立的成员,他们有自己的编码方式,程序设置为一个特定的路径,这是我用它的第一个项目。我有很多错误要纠正,这只是错误的路径。我现在要做的是扫描整个项目的单个目录(因为程序的开销目录在其使用的每个实例中都是常量),然后设置该目录的路径。为了简单化,让我们说它是一个Rails项目,所以Rails.root可能是开销,而搜索的目录是myawesomedir。任何帮助是极大的赞赏!

1 个解决方案

#1


1  

You can use the find stdlib:

你可以使用find stdlib:

require 'find'

Find.find(Rails.root) do |path|
  if File.basename(path) == 'myawesomedir'
    Dir.chdir path
    break
  end
end

#1


1  

You can use the find stdlib:

你可以使用find stdlib:

require 'find'

Find.find(Rails.root) do |path|
  if File.basename(path) == 'myawesomedir'
    Dir.chdir path
    break
  end
end