IronRuby QuickStart ReDuex

Back in January I did a post on getting started with IronRuby.  That post was based on Rev. 75 of the SVN tree.  As of  today (June 9th) the SVN tree is up to Rev. 113.  Obviously with things like RailsConf and TechEd driving a lot of the core teams work, a number of things have changed since January.  This post will basically be a rehash of the previous one, but updated for all the new quirks that have been introduced.

Getting The Source

As before, I recommend using TortoiseSVN to grab the source. Downloading will take a bit, after the 3.5MB of source is downloaded you should end up with a directory structure like the following:

image

Compiling IronRuby

Inside of the trunk directory you will find the IronRuby.sln file, double-clicking it will open the solution (note that the solution file is now in VS2008 format instead of the previous VS2005 format).  You may get a warning, you can select “Load the Project Normally” and uncheck “Ask me this for all projects in the solution.”

Once the solution is loaded, keep the solution on the “Debug” configuration.  Do NOT select “ExternalDebug” (you’ll get broken references if you do):

image

Next we will manually remove the “SIGNED” compilation symbol from all the projects, as of Rev. 113 you have to manually remove it from the IronRuby.Libraries, Microsoft.Scripting, and Microsoft.Scripting.Core (be sure not to delete the DLR symbol) projects.

image

Once those are removed you can build the source!

Running That Which You Have Wrought

Where has previously our compilation would output into a bin\Debug folder, the resulting files are now found in \trunk\build\debug, which should look like this:

image

Due to a conflict with rubinius, the rbx executable is now ir.

As before you can just run ir.exe and enter the wonderful world of ruby, or you can continue on to see how we roll in the .NET world.

Speaking C# With A Ruby Accent

The initial steps are basically the same as before, start a new C# console project and add references to Microsoft.Scripting.dll, Microsoft.Scripting.Core.dll, IronRuby.dll, and IronRuby.Libraries.dll.

We are going to create a simple console app that shows passing variables into and out of a ruby script.

The below program should be a good example:

 


using System;

using Ruby;

using Microsoft.Scripting.Hosting;
namespace IronRubyConsoleApp

{

    class Program

    {

        static void Main(string[] args)

        {
ScriptRuntime irruntime = IronRuby.CreateRuntime();

            ScriptEngine ir = IronRuby.GetEngine(irruntime);

            ScriptScope scope = ir.CreateScope();

            ScriptSource script = ir.CreateScriptSourceFromString(“puts \”Hello, #{name}!\”\ninput + 2″);

            scope.SetVariable(“name”, “.NET”);

            scope.SetVariable(“input”, 2);

            int x = script.Execute<int>(scope);

            Console.WriteLine(string.Format(“The Result was {0}”, x));

            Console.ReadLine();

        }

    }

}

You can see a clear hierarchy here:

We have a Runtime

That has an Engine

That has a Scope

That has a Source

We create a runtime, and we get our IronRuby engine into the runtime, we create a scope and load a script using that engine.  Set a couple of variables in the scope and then execute the script within the scope that we have set the variables in.

Hopefully this is enough to get you started, you should probably check out the other CreateScriptSource methods that the engine contains, you have a veritable cornucopia of options:

image

Ruby with a .NET Accent

Another of the popular activities with IronRuby, if not the most popular, is going to be interfacing with both the .NET framework and other .NET code.  The below example shows us interfacing with System.Windows.Forms and making a simple GUI app.

 

require ‘mscorlib’ require ‘ System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′ require ‘System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’ Swf=System::Windows::Forms Sd=System::Drawing class RubyForm <Swf::Form def add_button text, location button = Swf::Button.new button.Text = text button.Location = location self.Controls.Add button button end def initialize self.Text = “RubyForm” @rbutton = add_button “Click Me!”,  Sd::Point.new(150, 150) @rbutton2 = add_button “Click Me!”, Sd::Point.new(150, 100) @rbutton.Click {|sender, e| Swf::MessageBox.show ‘Hello World!’} @rbutton2.Click {|sender, e| Swf::MessageBox.show ‘Hello, .NET!’} end end rf = RubyForm.new rf.ShowDialog

As you can see above, when requiring items from the GAC we must include the Fully Qualified Name, including version and StrongNameToken if applicable. The above code inherits from System.Windows.Forms.Form and adds a helper class for adding buttons and wires up a couple of event handlers.  Again this is just a simple example to get you started.

Summary

In this post we downloaded the IronRuby source, compiled it, and worked with IronRuby both from C# and by running a ruby script against .NET objects.  Hopefully this gets you going a little faster and onto the fun stuff quicker!

Links

IronRuby Homepage

IronRuby RubyForge Project

Ruby Language Homepage

John Lam’s homepage

Cleveland Day of .NET: May 17, 2008

badgeEarlier today the website went live and registrations started. What is a “Day of .NET”? And why is it happening in Cleveland? A “Day of .NET” is, as the Day of .NET website says, “a one-day conference on all things .NET organized by developers for developers.” And that last part is why it’s happening in Cleveland. A number of us have frankly been tired of nothing happening in the Cleveland/North East Ohio area, and decided to take the bull by the horns as it were.

One of the themes we’re trying to push is that Day of .NET is .NET centric, but not .NET exclusive. So we’re hoping to bring a few speakers from other worlds (Java, Ruby, Python, etc) and learn from them. As well as explore a number of the exciting new things coming out of Microsoft these days (like the DLR, Silverlight, et. al.).

Interested in speaking? Contact speakers@clevelanddodn.org. Want to sponsor or know someone who would want to sponsor? We got lot’s of logo space to fill up, just contact sponsors@clevelanddodn.org. Most of all, get to www.clevelanddodn.org and register!

Detroit Launch Event, Post Event.

 

Tuesday (3/18) was the Microsoft Launch event in Detroit.  Being the intrepid geek I am, I decided to take a road trip and go to the event 3 hours away, rather then the one in the same town a week earlier.  Madness you say?  Nay, for I got to enjoy the good company of Corey Haines and Nate Hoellein on the way there and back as we car polled together.  While leaving at 4:30am was a bit rough, the drive was worth it once we got there.  I got to interact with a slew of smart people that I get to see far to rarely.   Dustin Campbell has a good list of everyone I ran into there (don’t worry Dustin, your secret of feeling up Jeff at lunch is safe with me).

The event du jour however was by far the Geek Dinner put on by Keith Elder, and generously sponsored by the Microsoft Visual Studio Team System..err. . Team.  If you’re ever in downtown Detroit and looking for some killer deep dish and wings, I can’t recommend PizzaPapalis enough.

After a cold beer and some hot pizza, it was time to take the long haul back to Cleveland.  Corey did a fantastic job getting us through the fog alive, and by the time I hit the bed around midnight I was more then ready for sleep. 

A New Day Dawns…

Microsoft is releasing the majority of their API and promising not to sue OpenSource for interoperability.  It’s brought of a "broad interoperability strategy".

All I can say is.  Wow.  If this is even half of what comes from all of this, Microsoft is no longer the one we all knew, real changes are being effected from inside.  Once I see the full details, I’ll post more complete thoughts. Details on the conference call here.

The Grass May Not Be Greener Yet, But The Soil Is More Fertile

 

The founder of the Ruby.NET project announced yesterday that he is leaving active development of that project and moving over to fully supporting the IronRuby effort.  Based on everything I’ve heard about Dr. Kelly and Ruby.NET, this is fantastic news.  Hopefully this will spurn even more activity on the IronRuby project.  Some of the important quotes from his post are:

"I’ve come to the conclusion that the DLR is clearly here to stay - it’s becoming an even more important part of the Microsoft platform."

"I now believe that IronRuby is more likely to succeed as a production quality implementation of Ruby on the .NET platform."

That’s quite an endorsement.  I have a feeling the next year or so will be the year of the DLR, we’ll begin seeing it’s API solidify with more and more languages being implemented on it.

That all said, IronRuby still has one major roadblock.  That is the perception of it’s corporate sponsor.  I had another dev tell me this today:

"Well, ironruby is mostly being  developed inside Microsoft, so almost everyone that talks about  ironruby dev day-to-day does so on  MS-internal mailing lists. "

John Lam say it ain’t so…

The ironruby-core mailing list is fairly active, and the MS-PL is OSI approved.  Yet, Microsoft has to actively be promoting these things, otherwise the outside perception will not change and IronRuby will be the less for it I fear.

What IronRuby needs more than anything right now is a large group of outside contributors, so come on down and get coding, testing, and documenting!

IronRuby Quick Start

IronRuby is Microsoft’s, with collaboration by the public, implementation of Ruby on their Ruby LogoDynamic Language Runtime. There’s another version of Ruby for .NET called, ironically, Ruby.NET that runs directly on the CLR. This post won’t be about that though, if you want to see a comparison, look here for a fairly good write up.

Downloading IronRuby from the SVN server and compiling in VS2005 was actually pretty painless. But after that I couldn’t find any, working, examples of getting an Ruby script running in the DLR. A big part of this is due to IronRuby still being officially “pre-Alpha” with the Scripting Host API in flux. Regardless, I hope this will be enough to get some people trying to use the latest SVN (rev. 75) up and running.

Building The Source

Download the latest revision from RubyForge using SVN, if you need a client I HIGHLY recommend TortiseSVN. Once you’re done downloading the source, you should be able to open the IronRuby.sln file in Visual Studio. We only need to make one change, and that’s to the Microsoft Scripting Project. Bring up the project properties and go to the Build tab:

signed.png

We have to remove the Conditional compilation symbol of “SIGNED”, otherwise the Scripting host will be looking for Microsoft signed copies of the IronRuby library, which we don’t have. After that go ahead and build the solution (cross your fingers if it makes you feel better).

When all is done you should end up with a bin\Debug folder in your SVN root that looks something like this:

debugdir.png

You can go ahead and start rbx from right there and begin playing with Ruby if you’d like. But if that’s all we wanted to do we would have just downloaded Ruby, right? This is IronRuby, let’s do it the .NET Way!


Hosting IronRuby in C#

Create a new Console Application solution in Visual Studio, say RubyExample. Add References to the Microsoft.Scripting.dll, IronRuby.dll, and IronRuby.Libraries.dll files.

Let’s begin with the most basic, a simple Hello World:
using System;using Ruby;using Ruby.Runtime;using Microsoft.Scripting;using Microsoft.Scripting.Hosting;namespace RubyExample{ static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { IScriptEnvironment scriptenvironment = ScriptEnvironment.GetEnvironment(); IScriptEngine rubyengine = scriptenvironment.GetEngine(“ruby”); scriptenvironment.ExecuteSourceUnit(rubyengine.CreateScriptSourceFromString(“puts’Hello World! \nPress Any Key To Continue..’”) ); Console.ReadKey(); } }
Let’s take a quick look at what we’re doing here. We’re setting up a ScriptEnvironment, this is where our Dynamic languages are going to live and play. Then out of that environment we’re asking for someone who understands Ruby. After that we’re just saying, hey ScriptEnvironment, run what the Ruby guy says.

So that’s pretty nifty, we could also tell the RubyEngine to CreateScriptSourceFromFile and move whatever code we want out of a string constant. Which is probably a good idea for anything beyond a line a two. But what if we actually want to talk back and forth? Let’s move on to the next example.

Accessing Global Variables

The easiest way to pass data between IronRuby and C# is via Global Variables. See below for an example.

As you can see I switched it up a bit and inited the Environment and Engine a bit differently, one way is more general, the other specific to IronRuby. The power of the ScriptingHost API is you can on the fly decide what language you want to use. But if you know you’re only going to be doing IronRuby, you can use the above method.

The real trick is in the second line, we get a reference to the GlobalVariables of the current IronRuby execution context. Once we have that we can start assigning global variables values and retrieving them back.

Summary

Hopefully this is enough to get you started. As I continue to delve into IronRuby, I will be sure to post what I find here, so keep an eye out!

IronRuby Homepage
IronRuby RubyForge Project
Ruby Language Homepage
DLR Hosting Spec
John Lam’s homepage

Changes At CodeBetter.

Looks like Glenn Block, from Microsoft’s patterns & practices group, is now blogging at CodeBetter. I’m sure it’s only a coincidence that Scott Bellware (ooold link, but only one still valid) no longer is (and had all his content removed).

Update: Glenn replied in the comments that Scott was actually the one that asked him to join CodeBetter.

Does xkcd Influence Microsoft Campus Design?

It appears that the new Microsoft Campus for the Entertainment and Devices Division is going to have a bar.  One wonders if they are not shooting for the “Ballmer Peak“…

Two Microsoft Licenses Now OSI Approved.

 

Am I blind, or is noone talking about this?  Microsoft finally getting two approved official Open Source licenses is big news.  With the approval the two licenses are being renamed:

Microsoft Permissive License (MS-PL) -> Microsoft Public License (MS-PL)

Microsoft Community License (MS-CL) -> Microsoft Reciprocal License (MS-RL)

 

via sburke, and now numerous other MSDN blogs now as well.

 

Seriously this must be being treated pretty low key, it’s certainly not being trumpeted from the towers like it should.  I heard many pundits say it would never happen…

The Microsoft .NET Framework Source Available for Developers!

Scott Guthrie just made an exciting post, starting with .NET 3.5 and VS 2008 the .NET libraries will have source available!  This is quite the boon to developers, the ability to drill down to source level while debugging should lead to more accurate code.  Of course there could be a downside, namely developers coding around implementation details rather then the exposed methods.  But regardless this is a very cool move by Microsoft.  It’s being released under the Microsoft Reference License which doesn’t give you a whole lot of rights (none really, other then to look at the source), but it’s something.  Considering how important the framework is to Microsoft this is a bold move.  For a truly permissive license check out Mono, here’s hoping there’s no “patent/copyright” pollution there either…