[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Sam Kong

6/21/2007 5:54:00 PM

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

1 Answer

Sami Samhuri

6/21/2007 6:05:00 PM

0

On 6/21/07, Sam Kong <sam.s.kong@gmail.com> 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.

--
Sami Samhuri