[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Dir.chdir("..") versus `cd ..`

pere.noel

12/11/2006 10:55:00 AM

i discovered this wornig that :

`cd ..` as no effect but :

Dir.chdir("..")

as the right effect, why isn't the first not working ?

i'm surprised because i'm doing a lot using back quotes )))

`rsync ... ` OK
`rm -rf / ` OK ;-)
and the like...

--
Père Noël
6 Answers

Vlad Galu

12/11/2006 11:09:00 AM

0

On 12/11/06, Père Noël <pere.noel@laponie.com.invalid> wrote:
> i discovered this wornig that :
>
> `cd ..` as no effect but :
>
> Dir.chdir("..")
>
> as the right effect, why isn't the first not working ?

Because "cd .." executes in a child process. So the cwd change only
takes place in it.

>
> i'm surprised because i'm doing a lot using back quotes )))
>
> `rsync ... ` OK
> `rm -rf / ` OK ;-)
> and the like...
>
> --
> Père Noël
>
>


--
If it's there, and you can see it, it's real.
If it's not there, and you can see it, it's virtual.
If it's there, and you can't see it, it's transparent.
If it's not there, and you can't see it, you erased it.

Patrick Plattes

12/11/2006 11:10:00 AM

0

Père Noël schrieb:
> i discovered this wornig that :
>
> `cd ..` as no effect but :
>
> Dir.chdir("..")
>
> as the right effect, why isn't the first not working ?

maybe the first one creates a new environment and close it after the
last backquotes, so you'll change the directory, but only in this
created environment.

Dir.chdir changes the directory in the current environment.

correct me, if I'm wrong,
patrick

Patrick Plattes

12/11/2006 11:11:00 AM

0

Père Noël schrieb:
> i discovered this wornig that :
>
> `cd ..` as no effect but :
>
> Dir.chdir("..")
>
> as the right effect, why isn't the first not working ?

maybe the first one creates a new environment and close it after the
last backquotes, so you'll change the directory, but only in this
created environment.

Dir.chdir changes the directory in the current environment.

correct me, if I'm wrong,
patrick

Patrick Plattes

12/11/2006 11:13:00 AM

0

Père Noël schrieb:
> i discovered this wornig that :
>
> `cd ..` as no effect but :
>
> Dir.chdir("..")
>
> as the right effect, why isn't the first not working ?

maybe the first one creates a new environment and close it after the
last backquotes, so you'll change the directory, but only in this
created environment.

Dir.chdir changes the directory in the current environment.

correct me, if I'm wrong,
patrick

Vincent Fourmond

12/11/2006 11:23:00 AM

0

Père Noël wrote:
> i discovered this wornig that :
>
> `cd ..` as no effect but :
>
> Dir.chdir("..")
>
> as the right effect, why isn't the first not working ?
>
> i'm surprised because i'm doing a lot using back quotes )))

When you're using backquotes, Ruby forks a shell and feeds it with the
command line given. With `cd ..`, you *do* change the current working
directory... for the forked shell. Not for the Ruby program... And the
forked shell dies immediately after the command is finished, so no way
of doing so. However, if you need to run only one command in another
directory, you might try:

`cd ..; a_nice_command`

Cheers,

Vince


--
Vincent Fourmond, PhD student
http://vincent.fourmon...

pere.noel

12/11/2006 11:39:00 AM

0

Vincent Fourmond <vincent.fourmond@9online.fr> wrote:

>
> When you're using backquotes, Ruby forks a shell and feeds it with the
> command line given. With `cd ..`, you *do* change the current working
> directory... for the forked shell. Not for the Ruby program... And the
> forked shell dies immediately after the command is finished, so no way
> of doing so. However, if you need to run only one command in another
> directory, you might try:
>
> `cd ..; a_nice_command`

yes, thanks for all (and to all answers) it's clear enough to me right
now )))

--
Père Noël