Iron Ruby DLR Sample

Recently, I created a very simple Iron Ruby sample for understanding how it works and what is the concept of DLR (Dynamic Language Runtime). DLR is built on top of CLR (Common Language Runtime). In .NET you can create apps which use IronRuby dynamic code and run them with the help of DLR.

You will need to download IronRuby from

In this sample, I just created a script.rb file which has some simple calls to one of the functions of my class. Then I created a console app. The code in my console app looks like this:
public class Program
        static void Main(string[] args)
            var worker = new CSharpWorker();

            var runtime = IronRuby.Ruby.CreateRuntime();
            var engine = runtime.GetEngine("Ruby");

            var scope = engine.CreateScope();
            scope.SetVariable("worker", worker);

            string fileName = @"C:\Demo\script.rb";
            var script = engine.CreateScriptSourceFromFile(fileName);


    public class CSharpWorker
        public void SaySomething(string str)
This code creates a runtime with Ruby engine and sets the scope of the script file to worker object. Once the execute is called, the code in script.rb file gets called. Code in script.rb file is a simple line: worker.SaySomething("Hi There")
When the script file runs, it outputs "Hi There". We can now change the script.rb file without the need of building the console exe.
We can create complex classes in the script.rb file and execute the same using this simple technique.