Shell <-> Ruby question

This is not a strictly Ruby question, but perhaps
you have some idea which could help me.

Recently I have written some some concise ruby scripts
which help me while working in the (bash) shell. (E.g.
x extracts any archive etc.)

I also wanted to write a more intelligent cd
(change sirectory) script in Ruby. Of course there is
an obvious problem I can’t solve: the bash opens
a new shell for the script, so the working directory
remains unchanged in the shell.

I also tried to run ruby with

… /usr/bin/ruby

I thought, it would run ruby in the current environment
(like ksh), but it simpy gives the error:

bash: ELF: command not found

Does someone have an idea, how could I change the
working directory by a Ruby script?

Regards, Christian

Hi,

Does someone have an idea, how could I change the
working directory by a Ruby script?

Fundamently, child processes can never modify directly parent
process’es environment. Unix process model prohibits it. You
can just receive child’s output and cd by the shell process
itself.

foo() {eval ruby somescript}

somescript

puts “cd #{directroy}”

···

At Sun, 27 Oct 2002 11:27:38 +0900, Christian Szegedy wrote:


Nobu Nakada

I also wanted to write a more intelligent cd
(change sirectory) script in Ruby. Of course there is
an obvious problem I can’t solve: the bash opens
a new shell for the script, so the working directory
remains unchanged in the shell.

How about ruby script only make a path to cd and do actual cd by shell
function or alias? Namely:

~> function mcd () {cd `ruby /tmp/foo.rb`}
~> cat > /tmp/foo.rb
puts "/tmp"
^D
~> mcd
/tmp>

Of course a script may do more useful thing, and a function/alias must
be more safe; at least a function/alias should check exit status of
the script and the script signals failure on such as no directory to
cd being found.

. /usr/bin/ruby

I thought, it would run ruby in the current environment

No, probably ksh also doesn’t. .' in sh-derived shells means run
the script in the current context. So `. /usr/bin/ruby’ evaluates
ruby itself as shell script.

···

In message apfia9$aq1$05$1@news.t-online.com szegedy@t-online.de writes:


kjana@dm4lab.to October 27, 2002
Behind the clouds is the sun still shining.

YANAGAWA Kazuhisa wrote:

How about ruby script only make a path to cd and do actual cd by shell
function or alias? Namely:

 ~> function mcd () {cd `ruby /tmp/foo.rb`}
 ~> cat > /tmp/foo.rb

Thanks! This was a great idea! It solves my problem.

Thank you for the help!

Regards, Christian