开发者

Resolving "require"s when executing IronRuby from C#

开发者 https://www.devze.com 2022-12-29 22:02 出处:网络
I\'m attempting to run an IronRuby script from C#: var runtime = IronRuby.Ruby.CreateRuntime(); runtime.ExecuteFile(\"ruby/foo.rb\");

I'm attempting to run an IronRuby script from C#:

var runtime = IronRuby.Ruby.CreateRuntime();                  
runtime.ExecuteFile("ruby/foo.rb");

foo.rb starts with a "requ开发者_如何学Pythonire:"

#!/usr/bin/env ruby
require 'bar'

When I try this, I get an exception stating "no such file to load -- bar." The file "bar.rb" and the directory "bar" are both present in the "ruby" directory.

So, how do I execute a ruby script that requires other ruby files? I'm targeting .Net 3.5.


Use the ScriptEngine instead and add your ruby code files directory to the search path:

var engine = IronRuby.Ruby.CreateEngine();

var paths = engine.GetSearchPaths().ToList();
paths.Add(@"C:\Path\To\My\Ruby\Files"); // Add the path to your ruby code files
engine.SetSearchPaths(paths);

engine.ExecuteFile("ruby/foo.rb");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号