To change directory inside a ruby script?

Viewed 75178

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.

7 Answers

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

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

outputs '/tmp'

Related