Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How can I implement cd (change dir) command in Ruby?

Reply
Thread Tools

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

 
 
Sam Kong
Guest
Posts: n/a
 
      06-21-2007
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

 
Reply With Quote
 
 
 
 
Sami Samhuri
Guest
Posts: n/a
 
      06-21-2007
On 6/21/07, Sam Kong <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
[RFC] How to implement command line tool integrating parsing engine llandre Python 2 03-22-2008 03:02 PM
Can I use winzip command in DOS command msmmgr Computer Support 8 10-05-2007 09:32 PM
How can I get a non 25x80 command window when I use the "at" command on XP? Please Computer Support 4 02-14-2007 12:01 AM
javascript command to implement button control Steve ASP .Net 4 05-28-2006 02:05 PM



Advertisments