ruby - 临时更改 Rake 中的当前目录

标签 ruby rake fabric

我想在另一个目录中运行多个命令(或更容易),然后在它们完成后返回到之前的工作目录。

我设想类似于 Fabric's with cd(path): 的东西,例如:

cd('.git') do
   File.unlink('config')
end

在 Rake 中是否有这样做的内置方法,或者我应该编写一个接受 block 的自定义方法等?

最佳答案

它只是内置的 Dir#chdir 调用:

Dir.chdir('.git') do
  File.unlink('config')
end

摘自 docs :

If a block is given, it is passed the name of the new current directory, and the block is executed with that as the current directory. The original working directory is restored when the block exits.

关于ruby - 临时更改 Rake 中的当前目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16533571/

相关文章:

css - 在使用 SASS 的现有元素中找不到 config.rb

python Fabric : How to handle arbitrary remote shell prompt for input?

regex - 从文本字符串中去除空格和\n

ruby-on-rails - 复杂的选择集,Rails 方式?

ruby-on-rails - rake 任务在另一个项目中执行 bundle 安装

ruby-on-rails - Rake 任务提示不安全的可写目录

ruby-on-rails - 如何设置用于播种的 Rake 任务

Python - 没有名为“fabric.api”的模块 - Windows 10

fabric - 了解 Fabric

ruby - 将 Ruby 打包成可移植的可执行文件