Click here to Skip to main content
12,405,868 members (70,503 online)
Click here to Skip to main content
Add your own
alternative version

Stats

17.2K views
203 downloads
6 bookmarked
Posted

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

, 26 Oct 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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(@"</span />add.rb"</span />
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("</span />Addition result from Iron Ruby method for 
    {0} and {1} is {2}"</span />
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
Rudra rafiq17-May-11 0:17
memberRudra rafiq17-May-11 0:17 
GeneralMy vote of 1 Pin
Batzen28-Oct-10 1:50
memberBatzen28-Oct-10 1:50 
GeneralMy vote of 1 Pin
AxelM27-Oct-10 20:51
memberAxelM27-Oct-10 20:51 
GeneralMy vote of 1 Pin
SledgeHammer0127-Oct-10 9:19
memberSledgeHammer0127-Oct-10 9:19 
GeneralFrom bad to worse Pin
kooshka27-Oct-10 8:35
memberkooshka27-Oct-10 8:35 
GeneralOk so you know how you emailed me and asked about what you could do better in your articles, well... Pin
Sacha Barber27-Oct-10 6:05
mvpSacha Barber27-Oct-10 6:05 
GeneralMy vote of 2 Pin
Argyle4Ever27-Oct-10 4:26
memberArgyle4Ever27-Oct-10 4:26 
GeneralMy vote of 1 Pin
JF201526-Oct-10 23:57
memberJF201526-Oct-10 23:57 
GeneralMy vote of 1 Pin
Oleg Shilo26-Oct-10 13:45
memberOleg Shilo26-Oct-10 13:45 
GeneralMy vote of 1 Pin
Selvin26-Oct-10 5:02
memberSelvin26-Oct-10 5:02 
GeneralMy vote of 1 Pin
Nagy Vilmos26-Oct-10 2:32
memberNagy Vilmos26-Oct-10 2:32 
GeneralMy vote of 5 Pin
moeinkiller200525-Oct-10 6:07
membermoeinkiller200525-Oct-10 6:07 
GeneralRe: My vote of 5 Pin
Niladri_Biswas25-Oct-10 19:41
memberNiladri_Biswas25-Oct-10 19:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160721.1 | Last Updated 27 Oct 2010
Article Copyright 2010 by Niladri_Biswas
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid