在 ruby​​ 脚本中更改目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3339883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:57:42  来源:igfitidea点击:

To change directory inside a ruby script?

ruby-on-railsruby

提问by never_had_a_name

I want to create a new rails application and fire up the rails server for that application, everything from a ruby script.

我想创建一个新的 rails 应用程序并为该应用程序启动 rails 服务器,一切都来自 ruby​​ 脚本。

My code look like this:

我的代码如下所示:

#!/usr/bin/env ruby
system "rails new my_app"
system "cd my_app"
system "rails server &"

However, when running "rails server &" the path is not in the my_app folder, but in the parent folder.

但是,当运行“rails server &”时,路径不在 my_app 文件夹中,而是在父文件夹中。

Is there a way to change directory inside a script so that i can run "rails server", "rake about" and "rake db:migrate" for that new application?

有没有办法更改脚本内的目录,以便我可以为该新应用程序运行“rails server”、“rake about”和“rake db:migrate”?

All work around tips would be appreciated.

所有解决技巧的工作将不胜感激。

回答by taw

Don't listen to them, Dir.chdir("dir")will probably do the wrong thing. What you almost always want is to limit change to a particular context, without affecting the rest of the program like this:

不要听他们的,Dir.chdir("dir")可能会做错事。您几乎总是想要的是将更改限制在特定的上下文中,而不像这样影响程序的其余部分:

#!/usr/bin/env ruby
system "rails new my_app"
Dir.chdir("my_app") do
  system "rails server &"
end
# back where we were, even with exception or whatever

回答by Adrian

Use Dir.chdir:

使用Dir.chdir

Dir.chdir "my_app"

回答by wondersz1

system supports :chdir argument that allows you to specify its working directory:

系统支持 :chdir 参数,允许您指定其工作目录:

system("echo Test; pwd", chdir: '/tmp')

outputs '/tmp'

输出'/tmp'

回答by mipadi

Use Dir.chdirto change the working directory for a script.

Dir.chdir改变工作目录的脚本。

回答by user402987

Use Dir.chdir("[aString]")

使用Dir.chdir("[aString]")

回答by Gaurav Manchanda

Why can't you just do it like this:

为什么你不能这样做:

#!/usr/bin/env ruby
system 'rails new myapp && cd myapp && rails server &'

回答by itsnikolay

The following lines have the same output:

以下几行具有相同的输出:

puts Dir.chdir("/tmp") { IO.popen("ls -la") { |io| io.read } } 

puts IO.popen(["ls", "-la", "/tmp"]).read

puts IO.popen("ls -la /tmp").read

# drwxrwxrwt 25 root       root       16384 июля  23 01:17 .
# drwxr-xr-x 22 root       root        4096 июля  22 13:36 ..
# drwxrwxr-x 12 itsnikolay itsnikolay  4096 июля  19 17:14 app_template
# drwx------  2 itsnikolay itsnikolay  4096 июля  21 15:04 .com.google.Chrome.dThb8f
# drwx------  2 itsnikolay itsnikolay  4096 июля  18 20:55 .com.google.Chrome.FGDBGc

also you can run rails and create an application (this can be helpful in rspec tests and etc.):

你也可以运行 rails 并创建一个应用程序(这在 rspec 测试等中很有帮助):

IO.popen("cd /tmp/ && rails new test_app").read

and ever you can run a rails server ;)

并且您可以运行 Rails 服务器;)