115

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

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.

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?

All work around tips would be appreciated.

1
  • 6
    Note that the best answer is not the marked one. Keep reading to the bottom of this page.
    – Zane
    Commented Mar 10, 2014 at 15:38

7 Answers 7

397

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:

#!/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
4
  • 1
    Be very careful using a block in a threaded application such as one that uses sidekiq. Just and FYI :) Commented Oct 10, 2014 at 20:42
  • @jryancanty If I make a thread, and change directory like in this answer, what are the "side effects," if any? (I will not use sidekick, just Thread class)
    – user3373470
    Commented Aug 13, 2015 at 13:33
  • 1
    I am also curious as to what the side effects are (from using either method). If you change the working directory, you can always change it back, right?
    – stevec
    Commented Feb 17, 2019 at 5:12
  • Don't know of any times you can't...just do original_directory = Dir::pwd, then recall the variable later like so: Dir::chdir(original_directory)
    – Poyda
    Commented Jan 30, 2020 at 23:09
112

Use Dir.chdir:

Dir.chdir "my_app"
3
  • 22
    Keep in mind this can have side-effects throughout the rest of your program. See other answers to avoid potential bugs from this.
    – Leopd
    Commented Mar 16, 2012 at 19:07
  • This is a bad answer. It solves the problem, but you can have a problem in code after it. Please consider to change "best answer" to another one (with more upvotes)
    – Ezh
    Commented Nov 12, 2019 at 13:39
  • Worked well, but when the script is over, the shell is still in its initial directory. Any way to leave the script in that other directory we changed into?
    – Adeynack
    Commented Mar 5, 2022 at 9:25
17

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

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

outputs '/tmp'

3

Use Dir.chdir to change the working directory for a script.

1

Why can't you just do it like this:

#!/usr/bin/env ruby
system 'rails new myapp && cd myapp && rails server &'
1
  • 1
    When the system command fails, it will be harder to diagnose what went wrong -- did the rails app fail, did the cd fail, or did rails server fail?
    – ablarg
    Commented Oct 22, 2015 at 16:58
1

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.):

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

and ever you can run a rails server ;)

0

Use Dir.chdir("[aString]")

Not the answer you're looking for? Browse other questions tagged or ask your own question.