2

It was answered here calling a ruby script in c#

but does that work? I tried this but it keeps failing with "The system cannot find the file specified" error, I'm assuming its because of ruby command before the file name, but I'm not quite sure.

Thanks for the help

1
  • 1
    if you have ruby installed and the file's path in the code ruby C:\ruby_script.rb is correct, it should work.. Commented Mar 9, 2011 at 19:49

4 Answers 4

4

You could also try to execute the Ruby code with IronRuby with something like this

using System;
using Microsoft.Scripting.Hosting;
using IronRuby;

class ExecuteRubyExample
{
    static void Main()
    {
        ScriptEngine engine = IronRuby.Ruby.CreateEngine();
        engine.ExecuteFile("C:/rubyscript.rb");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I did check out IR however sadly I'm still running on 3.5 and didn't want to compile the IR code from source, it is a great option however, Thanks
1

The linked answer looks reasonably proper, but it's obviously not working for you. That means it's probably one of two things.

1) the backslashes are biting you. Try changing

ProcessStartInfo info = new ProcessStartInfo("ruby C:\rubyscript.rb");

to

ProcessStartInfo info = new ProcessStartInfo(@"ruby C:\rubyscript.rb");

or

ProcessStartInfo info = new ProcessStartInfo("ruby C:\\rubyscript.rb");

The first change uses string literals, the second escapes the backslash properly.

2) the environment path isn't getting Ruby's bin directory exported to it. This is less likely and more of a pain to test for, so I'd focus on the first.

3 Comments

Nice answer; but on point 2, recent versions of Windows give you the where command, which makes path issues pretty easy to sort out. Just run where ruby and you'll get a list of things in the path that match the pattern. If nothing matches, you'll get an error.
@Iceman - thanks for the tip on the where command - I didn't know about it. Unfortunately, I'm stuck on WinXP at work, so that does me no good.
Thanks for the help guys, for some reason the way it was written in the example did not work for me the way I finally got it to work is doing this ProcessStartInfo rubyProc = new ProcessStartInfo(@"ruby"); rubyProc.Arguments = @"C:\rubytest.rb";
0

Here is my code for running a ruby script.

using (var proc = new Process())
{
    var startInfo = new ProcessStartInfo(@"ruby");
    startInfo.Arguments = filePath;
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true;
    proc.StartInfo = startInfo;
    proc.Start();
}

This method runs it asynchronously, since the script may take an unknown amount of time to run this allows the main thread to keep running without locking it, and then waits for the script to finish running before returning the Task.

private async Task RunRubyScript(string filePath)
{
  await Task.Run(() =>
  {
    using (var proc = new Process())
    {
      var startInfo = new ProcessStartInfo(@"ruby");
      startInfo.Arguments = filePath;
      startInfo.UseShellExecute = false;
      startInfo.CreateNoWindow = true;
      proc.StartInfo = startInfo;
      proc.Start();
      proc.WaitForExit();
    }
  });
}

Hope this helps!

Comments

0

Try this

void runScript()
{
    using (Process p = new Process())
    {
        ProcessStartInfo info = new ProcessStartInfo("ruby");
        info.Arguments = "C:\rubyscript.rb args"; // set args
        info.RedirectStandardInput = true;
        info.RedirectStandardOutput = true;
        info.UseShellExecute = false;
        p.StartInfo = info;
        p.Start();
        string output = p.StandardOutput.ReadToEnd();
        // process output
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.