How can I use Ruby code in .NET?

I would like to use RubyGem in my C # application.

I downloaded IronRuby, but I'm not sure how to get up and work. Their download includes the ir.exe file and includes some DLL files such as IronRuby.dll.

Once the IronRuby.dll link is listed in my .NET project, how do I show the objects and methods of the * .rb file in my C # code?

Thank you very much,

Michael

+4
source share
1 answer

Here's how you interact:

Make sure you have links to IronRuby , IronRuby.Libraries , Microsoft.Scripting and Microsoft.Scripting.Core

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using IronRuby; using IronRuby.Builtins; using IronRuby.Runtime; namespace ConsoleApplication7 { class Program { static void Main(string[] args) { var runtime = Ruby.CreateRuntime(); var engine = runtime.GetRubyEngine(); engine.Execute("def hello; puts 'hello world'; end"); string s = engine.Execute("hello") as string; Console.WriteLine(s); // outputs "hello world" engine.Execute("class Foo; def bar; puts 'hello from bar'; end; end"); object o = engine.Execute("Foo.new"); var operations = engine.CreateOperations(); string s2 = operations.InvokeMember(o, "bar") as string; Console.WriteLine(s2); // outputs "hello from bar" Console.ReadKey(); } } } 

Note. Runtime has an ExecuteFile that you can use to execute your file.

To get Gems go

  • Make sure you install your gem using igem.exe
  • you may have to set up multiple search paths using Engine.SetSearchPaths
+4
source

All Articles