Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To change directory inside a ruby script?

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.

like image 679
never_had_a_name Avatar asked Jul 27 '10 00:07

never_had_a_name


People also ask

How to make directory in Ruby?

Using Ruby's Mkdir Method To Create A New Directory If you want to create a new folder with Ruby you can use the Dir. mkdir method. If given a relative path, this directory is created under the current path ( Dir. pwd ).

What does CD mean in Ruby?

cd means change directory.


1 Answers

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 
like image 77
taw Avatar answered Oct 03 '22 05:10

taw