C# running ruby scripts give error: no such file to load

Hello ironruby user,

Here I am with another question. This is a problem I ran in a few weeks
ago, found a workaround, but this workaround is far from perfect.

As some might already know, I am building an C# application and am using
ironruby to run rubyscripts.

Application located in:
D:\app\bin

Testscripts located in:
K:\Testscripts\Specific

These testscripts uses custom made libraries located in:
K:\Testscripts\Common

But also they use standard ruby libraries located in:
C:\Ruby\lib…

So the top of the testscripts can look someting like:

Testscript1.rb
require ‘…/Common/LibTestCase1’
require ‘…/Common/LibTestCase2’

or

Testscript2.rb
require ‘win32ole’
require ‘ftools’

Now the problem:

Running these these testscripts from the C# application (using e.g.
engine.ExecuteFile) results in the error: no such file to load –
‘…/Common/LibTestCase1’ or ‘win32ole’ or whatever file is required.
It seems the scripts (or application) is trying to located the file
relative to the path the application is located.

This leads me to the workaround. Before running the testscript from the
application change the current directory of the application to the
location of the testscripts (using Environment.CurrentDirectory =
K:\Testscripts\Specific)

Since testscripts can be located deeper in the directory stucture I need
to keep changing the current directory of the application. You’ll
understand that this is a pretty sucky workaround.

I have tried using SetLoadPath to include the directories where the
required files can be found, but this still gives the same error.

So I am looking for a more flexibel solution for this problem. Hopefully
someone here provide information to stear me in the correct direction.

Thanks in advance

File.dirname(FILE) will give you the directory of the current file,
so you can place this in-front of your requires to make sure they are
always relative to the current file, rather than the current directory:

Testscript1.rb:
require File.dirname(FILE) + ‘/…/Common/LibTestCase1’
require File.dirname(FILE) + ‘/…/Common/LibTestCase2’

You could also add “File.dirname(FILE) + ‘/…/Common/’” to the path
like this:

$: << File.dirname(FILE) + ‘/…/Common/’

So then requires can just be:

require ‘LibTestCase1’
require ‘LibTestCase2’

These are just issues related to how Ruby deals with relative paths and
the current directory, regardless of whether you ran the code from C# or
not. However, it should find win32ole just fine … do you have an
app.config for your C# app which sets the load path (see ir.exe.config
for example).

~js

Thanks, I wasn’t aware I should set all loadpaths from within the
application to use the ‘win32ole’.

Now it finds the win32ole, but it gives an error when trying to execute
the require.

On require ‘win32ole’ the following error is displayed.
The module was expected to contain an assembly manifest. (Exception from
HRESULT: 0x80131018)

I have no idea what this means.

Seemed the script required the ‘win32ole’ from Ruby and not IronRuby.
Using the IronRuby win32ole class does not give any errors.

Eelco Henderichs wrote:

Thanks, I wasn’t aware I should set all loadpaths from within the
application to use the ‘win32ole’.

Now it finds the win32ole, but it gives an error when trying to execute
the require.

On require ‘win32ole’ the following error is displayed.
The module was expected to contain an assembly manifest. (Exception from
HRESULT: 0x80131018)

I have no idea what this means.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs