Calling Ruby from within Ruby under Windows

R

Ronald Fischer

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.=20

--=20
Ronald Fischer <[email protected]>
Phone: +49-89-452133-162
=20
 
N

Nobuyoshi Nakada

Hi,

At Thu, 21 Jun 2007 20:56:49 +0900,
Ronald Fischer 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.
 
R

Ronald Fischer

At Thu, 21 Jun 2007 20:56:49 +0900,
Ronald Fischer wrote in [ruby-talk:256378]:
I get the error message:
=20
H:/tmp/x.rb:1:in ``': Exec format error - t2.rb (Errno::ENOEXEC)
from H:/tmp/x.rb:1
=20
Is this a bug or intended behaviour?
=20
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.
=20
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top