为什么“需要”?通过 C# 程序加载 (iron)ruby 脚本时语句失败?

发布于 2024-08-23 12:58:20 字数 890 浏览 8 评论 0原文

IronRuby 和 VS2010 菜鸟问题:

我正在尝试做一个峰值来测试 C# 项目和现有 RubyGem 之间互操作的可行性,而不是在 .net 中重新发明那个特定的轮子。我已经下载并安装了 IronRuby 和 RubyGems 包,以及我最终想要使用的 gem。

运行 .rb 文件或在 iirb Ruby 控制台中工作没有问题。我可以加载 RubyGems 包和 gem 本身并使用它,因此,至少对于该用例,我的环境设置正确。

但是,当我尝试在 C# (4.0) 控制台应用程序中执行相同的操作时,它会抱怨第一行:

require 'RubyGems'

出现错误:

 no such file to load -- rubygems

我的控制台应用程序如下所示:

using System;
using IronRuby;
namespace RubyInteropSpike
{
    class Program
    {
        static void Main(string[] args)
        {
            var runtime = Ruby.CreateRuntime();

            var scope = runtime.ExecuteFile("test.rb");
            Console.ReadKey();
        }
    }
}

删除依赖项并仅执行一些基本的自我操作-包含的 Ruby 东西工作正常,但包含任何类型的“requires”语句似乎会导致它失败。

我希望在创建 ruby​​ 运行时时只需要传递一些附加信息(路径等),并且真的希望这不是某种限制,因为这会让我感到难过。

IronRuby and VS2010 noob question:

I'm trying to do a spike to test the feasibility of interop between a C# project and an existing RubyGem rather than re-invent that particular wheel in .net. I've downloaded and installed IronRuby and the RubyGems package, as well as the gem I'd ultimately like to use.

Running .rb files or working in the iirb Ruby console is without problems. I can load the both the RubyGems package, and the gem itself and use it, so, at least for that use case, my environment is set up correctly.

However, when I try to do the same sort of thing from within a C# (4.0) console app, it complains about the very first line:

require 'RubyGems'

With the error:

 no such file to load -- rubygems

My Console app looks like this:

using System;
using IronRuby;
namespace RubyInteropSpike
{
    class Program
    {
        static void Main(string[] args)
        {
            var runtime = Ruby.CreateRuntime();

            var scope = runtime.ExecuteFile("test.rb");
            Console.ReadKey();
        }
    }
}

Removing the dependencies and just doing some basic self-contained Ruby stuff works fine, but including any kind of 'requires' statement seems to cause it to fail.

I'm hoping that I just need to pass some additional information (paths, etc) to the ruby runtime when I create it, and really hoping that this isn't some kind of limitation, because that would make me sad.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给不了的爱 2024-08-30 12:58:20

简短的回答:是的,这将按照您想要的方式工作。
您需要使用引擎的 SetSearchPaths 方法来执行您想要的操作。

更完整的示例
(假设您将 IronRuby 加载到 C:\IronRubyRC2 作为根安装目录)

    var engine = IronRuby.Ruby.CreateEngine();

    engine.SetSearchPaths(new[] {
        @"C:\IronRubyRC2\Lib\ironruby",
        @"C:\IronRubyRC2\Lib\ruby\1.8",
        @"C:\IronRubyRC2\Lib\ruby\site_ruby\1.8"
    });

    engine.Execute("require 'rubygems'"); // without SetSearchPaths, you get a LoadError
    /*
    engine.Execute("require 'restclient'"); // install through igem, then check with igem list
    engine.Execute("puts RestClient.get('http://localhost/').body");
    */
    Console.ReadKey();

Short answer: Yes, this will work how you want it to.
You need to use the engine's SetSearchPaths method to do what you wish.

A more complete example
(Assumes you loaded your IronRuby to C:\IronRubyRC2 as the root install dir)

    var engine = IronRuby.Ruby.CreateEngine();

    engine.SetSearchPaths(new[] {
        @"C:\IronRubyRC2\Lib\ironruby",
        @"C:\IronRubyRC2\Lib\ruby\1.8",
        @"C:\IronRubyRC2\Lib\ruby\site_ruby\1.8"
    });

    engine.Execute("require 'rubygems'"); // without SetSearchPaths, you get a LoadError
    /*
    engine.Execute("require 'restclient'"); // install through igem, then check with igem list
    engine.Execute("puts RestClient.get('http://localhost/').body");
    */
    Console.ReadKey();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文