Running Ruby scripts from C# application
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Running Ruby scripts from C# application

  1. #1
    Join Date
    Jul 2009
    Posts
    5

    Running Ruby scripts from C# application

    What I am trying to achieve is creating a C# application that wil run several Ruby scripts from a several locations.

    In my C# project I have added references to several .dll files (e.g. IronRuby.dll, IronRuby.Libraries.dll, Microsoft.Scripting.dll, etc) in order to execute the ruby scripts.

    Location scripts: D:\Projects\Ruby\Test
    Location C# app: D:\Projects\VisualStudio\C#\Test

    Example RubyScript.rb:
    require 'Test'

    aTest = Test.new
    puts aTest.attr

    Code used to call Ruby scripts:
    var runtime = Ruby.CreateRunTime();
    var engine = runtime.GetEngine();

    engine.ExecuteFile("D:\\Projects\\Ruby\\Test\\RubyScript.rb");

    Running the application results in the following error message:
    - No such file or directory - Test

    The application wil run without errors if:
    - Ruby scripts do not use require
    - The require is defined using absolute paths
    - Application is in same directory as the Ruby scripts

    I do not like any of these solutions very much. I would like the application to know where to look for the required ruby classes/scripts. Is this possible or do I need to find another solution for my application?

    All information on this problem/subject/solution is welcome.

    Thanks in advance

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,202

    Re: Running Ruby scripts from C# application

    Is there some sort of path that you can set in Ruby to find the Test location?

  3. #3
    Join Date
    Jul 2009
    Posts
    5

    Re: Running Ruby scripts from C# application

    Quote Originally Posted by Arjay View Post
    Is there some sort of path that you can set in Ruby to find the Test location?
    Yes, this path is set for Ruby via the environment variable RUBYPATH (or something). Running the ruby scripts from command line using Ruby is not a problem. Running them from the C# application gives error message.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,202

    Re: Running Ruby scripts from C# application

    So there isn't a way to programmatically set this environment variable within the ruby engine?

    If not, I wonder if your process is picking up this variable.

    Inside your code, try
    Code:
     
    string envRubyPath = Environment.GetEnvironmentVariable( "RUBYPATH" );

  5. #5
    Join Date
    Jul 2009
    Posts
    5

    Re: Running Ruby scripts from C# application

    Quote Originally Posted by Arjay View Post
    So there isn't a way to programmatically set this environment variable within the ruby engine?

    If not, I wonder if your process is picking up this variable.

    Inside your code, try
    Code:
     
    string envRubyPath = Environment.GetEnvironmentVariable( "RUBYPATH" );
    This code returns the path where the ruby scripts and classes are located.

  6. #6
    Join Date
    May 2007
    Posts
    1,546

    Re: Running Ruby scripts from C# application

    Do you need to set the working directory to the same path as the script before you execute it? Environment.CurrentDirectory does it if i remember correctly.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  7. #7
    Join Date
    Jul 2009
    Posts
    5

    Re: Running Ruby scripts from C# application

    Quote Originally Posted by Mutant_Fruit View Post
    Do you need to set the working directory to the same path as the script before you execute it? Environment.CurrentDirectory does it if i remember correctly.
    Thanks very much. This seems to work nicely. I wasn't aware you were allowed to change the working directory dynamically. Does this cause any side effects?

  8. #8
    Join Date
    May 2007
    Posts
    1,546

    Re: Running Ruby scripts from C# application

    Things to look out for would be that if you open files using a relative path, the path is always relative to the working directory. Some classes in System.Windows.Forms, like the FileChooser, can default to using the current directory aswell. Also if you wanted to run multiple scripts simultaenously you may run into issues unless they are ain the same directory.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center