Question

I'm attempting to re-use some ruby classes I wrote a while back within an ASP.NET MVC 2 project. The issue I'm having is if a class is within a Module I can't seem to instantiate it. If I move the class outside of the module it works fine. Here is a scaled down version of the class I want to instantiate:

module Generator
  class CmdLine
    attr_accessor :options

    def initialize(output)
    end

    def run(args=[])
    end
  end
end

If comment out the module portion I can create the object. Am I doing something wrong? Here is the C# code:

var engine  = Ruby.CreateEngine();
var searchPaths = engine.GetSearchPaths().ToList();
searchPaths.Add(@"c:\code\generator\lib");
searchPaths.Add(@"C:\Ruby-ri-192\lib\ruby\1.9.1");
engine.SetSearchPaths(searchPaths);

engine.ExecuteFile(@"c:\code\generator\lib\generator\generator_cmd_line.rb");
var rubyCmdLineObj = engine.Runtime.Globals.GetVariableNames();
// These lines works when I comment out the module
// var genCmdLineObj = engine.Runtime.Globals.GetVariable("CmdLine");
// var cmdLineObj = engine.Operations.CreateInstance(genCmdLineObj);
// var results = engine.Operations.InvokeMember(cmdLineObj, "run");
//  return Content(results);
var sb = new StringBuilder();
foreach (var name in rubyCmdLineObj)
{
  sb.AppendFormat("{0} ", name);
}

return Content(sb.ToString());

I have a work around - creating a separate class that I can call from within C# but if I don't have to do that I'd rather not do it. Any guidance would be greatly appreciated.

Was it helpful?

Solution

I know this is kind of a hack/workaround, but I managed to do it this way:

Add the next code to the end of your ruby file:

def hack(s)
  eval(s)
end

Now your C# code would look like that:

var engine = Ruby.CreateEngine();

var scope = engine.ExecuteFile(@"c:\code\generator\lib\generator\generator_cmd_line.rb");

var genCmdLineObj = engine.Execute(String.Format("hack('{0}::{1}')", "Generator", "CmdLine"), scope);
var cmdLineObj = engine.Operations.CreateInstance(genCmdLineObj);
var results = engine.Operations.InvokeMember(cmdLineObj, "run");
return Content(results);

Kind of a hack, but hey, it works! :)

OTHER TIPS

I would create a new IronRuby project, take your original Ruby code and port/compile it into a .NET library.

No more need to call out at all. You have something that can be called natively from C#.

The solution @Shay Friedman proposed is unnecessary.

gen.rb

module Generator
  class CmdLine
    attr_accessor :options

    def initialize(output)
        @output = output
    end

    def run(args=[])
        puts "Hello from cmdLine with #{@output} #{args}"
    end
  end
end

csharp

void Main()
{
    var engine = Ruby.CreateEngine();
    var buffer = new MemoryStream();
    engine.Runtime.IO.SetOutput(buffer, new StreamWriter(buffer));
    engine.ExecuteFile(@"c:\temp\gen.rb");
    ObjectHandle handle = engine.ExecuteAndWrap("Generator::CmdLine.new('the output')");
    var scope = engine.CreateScope();
    scope.SetVariable("myvar", handle);
    engine.Execute("myvar.run", scope);
    engine.Operations.InvokeMember(handle.Unwrap(), "run", "InvokeMember");
    buffer.Position = 0;
    Console.WriteLine(new StreamReader(buffer).ReadToEnd());
}

output

Hello from cmdLine with the output []
Hello from cmdLine with the output InvokeMember
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top