How can I implement cd (change dir) command in Ruby?

Hi,

This question is rather out of curiosity than practical.

Let’s say I want to make a script which is equivalent to cd command in
Linux.
Is it possible?

Usage:

~$ ruby mycd.rb /temp
/temp$ <- current dir has been changed after the script ran.

system(“cd #{ARGV[0]}”) didn’t work.

Thanks.

Sam

On 6/21/07, Sam K. [email protected] wrote:

Hi,

This question is rather out of curiosity than practical.

Let’s say I want to make a script which is equivalent to cd command in
Linux.
Is it possible?

Only if your shell is done in Ruby as well. chdir(2) changes the
directory for the process which invokes it. cd is a shell built-in,
one which has no analog in /bin.

system(“cd #{ARGV[0]}”) didn’t work.

That will spawn a new process running /bin/sh. That shell will change
its directory and then exit. afaik what you want is not possible
without resorting to (ugly) trickery.

Hope this helps.