# Calling Ruby from within Ruby under Windows

**URL:** <https://www.ruby-forum.com/t/calling-ruby-from-within-ruby-under-windows/107405>\
**Category:** Ruby\
**Created:** [June 21, 2007, 1:58pm UTC](https://www.ruby-forum.com/t/calling-ruby-from-within-ruby-under-windows/107405 "2007-06-21T13:58:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 21, 2007, 1:58pm UTC](https://www.ruby-forum.com/t/calling-ruby-from-within-ruby-under-windows/107405/1 "2007-06-21T13:58:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 21, 2007, 10:48pm UTC](https://www.ruby-forum.com/t/calling-ruby-from-within-ruby-under-windows/107405/2 "2007-06-21T22:48:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [June 22, 2007, 11:14am UTC](https://www.ruby-forum.com/t/calling-ruby-from-within-ruby-under-windows/107405/3 "2007-06-22T11:14:55Z")

</div>

> 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
