Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Step by step guidance for calling a IronRuby Method from C#4.0

0.00/5 (No votes)
27 Oct 2010 1  
A short demo as how to call a method written in IronRuby 1.1 and making a dynamic invocation to the method from C# environment.

Introduction

IronRuby is a Open Source implementation of the Ruby programming language for .NET, heavily relying on Microsoft's Dynamic Language Runtime(Quoted from IronRuby Site

This small article will demonstrate step by step as to how we can call IronRuby 1.1 function from C#4.0 using the dynamic keyword

Using the code

Step 1: Download the latest version of Iron Ruby from Code Plex

Step 2: Install the software.

Step 3: For this demo we are using add.rb Ruby file which has only one Add method that performs addition of two numbers being supplied as parameters to the method.

Step 4: We are using a console application for the demo purpose. The very first thing we need to do is to add the dlls as show below

1.jpg

Step 5: The function invocation happens in just three steps as shown below

class Program
    {
        static void Main(string[] args)
        {
            //Step 1: 
            //Creating a new script runtime   
            var ironRubyRuntime = Ruby.CreateRuntime();

            try
            {
                //Step 2:
                //Load the Ruby file/script into the memory
                //Should be resolve at runtime
                dynamic loadIRuby = ironRubyRuntime.UseFile(@"add.rb");

                //Step 3:
                //Invoke the method and print the result
                Console.WriteLine(
               string.Format("Addition result from Iron Ruby method for {0} and  
               {1} is {2}",100, 200, loadIRuby.add(100, 200)));               
            }
            catch (FileNotFoundException ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey(true);
        }
    }

The very first line

var ironRubyRuntime = Ruby.CreateRuntime();
loads the Iron Ruby libraries along with the DLR code needed to execute the python script. Second line
dynamic loadIRuby = ironRubyRuntime.UseFile(@"add.rb");
loads the Ruby script into memory. The significance of the dynamic variable(loadIRuby) here is being the fact that Ruby calls are resolve at runtime. The last line i.e.
Console.WriteLine(
     string.Format("Addition result from Iron Ruby method for 
    {0} and {1} is {2}", 100, 200, 
    loadIRuby.add(100, 200)));
Is simply to invoke the Ruby method and to display the result. Step 6: And here is the output 3.jpg

References

Getting Started With IronRuby

Conclusion:

In this short tutorial we have seen how to invoke IronRuby methods from C# 4.0.

Comments on the topic are highly appreciated for the improvement of the topic.

Thanks for reading the article.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here