Forum: Ruby Calling Ruby from within Ruby under Windows

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C667185722eaecf12461cb423dc8b410?d=identicon&s=25 Ronald Fischer (Guest)
on 2007-06-21 13:58
(Received via mailing list)
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.
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (Guest)
on 2007-06-21 22:48
(Received via mailing list)
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.
C667185722eaecf12461cb423dc8b410?d=identicon&s=25 Ronald Fischer (Guest)
on 2007-06-22 11:14
(Received via mailing list)
> 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
This topic is locked and can not be replied to.