views:

48

answers:

4

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.

Thanks!

+2  A: 

Use Dir.chdir:

Dir.chdir "my_app"
Adrian
Adrian - beat me to it. After I typed it, I went to go fetch the link to the RubyDocs. Rather than answering, I just threw the link into your answer.
Thomas Owens
A: 

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

mipadi
A: 

Use Dir.chdir("[aString]")

A: 

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

#!/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
taw