Calling Ruby from within Ruby under Windows


#1

Ruby 1.8.6 after installation has associated the file extension .rb
within Windows to Ruby. Hence I can call a Ruby program directly
from the Windows command line:

ruby t2.rb
t2.rb
t2

all work the same.

This does not work, however, if the program is executed from
within Ruby itself. Example:

Assuming I have a file t2.rb containing only the line

puts “hello”

and another file containing x.rb containing only the line

%x(t2.rb)

When I execute on the Windows command line

x.rb

I get the error message:

H:/tmp/x.rb:1:in ``’: Exec format error - t2.rb (Errno::ENOEXEC)
from H:/tmp/x.rb:1

Is this a bug or intended behaviour?

Changing x.rb to

%x(ruby t2.rb)

works well.


#2

Hi,

At Thu, 21 Jun 2007 20:56:49 +0900,
Ronald F. wrote in [ruby-talk:256378]:

I get the error message:

H:/tmp/x.rb:1:in ``’: Exec format error - t2.rb (Errno::ENOEXEC)
from H:/tmp/x.rb:1

Is this a bug or intended behaviour?

Not a bug. Cmd.exe uses ShellExecute or similar, which see the
association. You may want to say, “then use it in ruby too”,
but no. It doesn’t handle redirection.

Once I had tried changing to see the association, it was very
tiresome and others didn’t seem to like to merge it.


#3

association. You may want to say, “then use it in ruby too”,
but no. It doesn’t handle redirection.

Once I had tried changing to see the association, it was very
tiresome and others didn’t seem to like to merge it.

I understand.

In any case, maybe it makes sense to look at the issue from a completely
different point of view: What is the best way to call an external
program
(maybe written in Perl, Ruby, Python or whatever) from within
Ruby, under the conditions that

  • The resulting “multi-language” application should be easy to port
    between
    Windows and Unix/Linux, and

  • The program should be looked up using PATH.

On Unix, it is easy of course, because both can be handled by the shell
via
which the external program can be invoked. On Windows, this seems to
work in
a similar way if I invoke a external program from the Windows command
line.
When doing it from Ruby, I would have to manually search the PATH for a
matching program, then determine, which language the program is (Ruby,
Perl,…),
and execute that language interpreter, handing it as argument the path
of
the program.

This certainly can be done, but I wonder whether this is not a
sufficiently
general problem, that it should make it somehow into Ruby. I can
understand
that you don’t want to go this “magic” into the default versions of
%x(…)
and system(…), but maybe it should be offered as an option, for
instance
controlled via some switch?

Ronald