In a previous post I showed you how to pull down the latest source for IronRuby and get it built in Visual Studio. Well, today I am going to look at how you would write and run programs using IronRuby. I am trying out a new source syntax highlighter, since my old one doesn’t support Ruby. So if you are having problems with the source, then just view it on my site instead of an RSS reader.

Okay, so the first thing you are going to have to do now is kick Visual Studio to the curb. Visual Studio rocks, but it has no support for Ruby. So, go ahead and close it. No seriously, just close it, I’ll wait…

Okay, done? Good. Now you are probably going to want an editor that has source highlighting for Ruby. It just makes life a little easier, and who doesn’t want that when learning a new language? I recommend grabbing Notepad++ if you don’t have a particular tool in mind. It is a great editor and it has built in support for syntax highlighting. Best of all, its free.

So, first off make sure that you did everything in the last post, including adding the location of ir.exe to your path. This is important, otherwise you will have some problems.

Next we are going to just create a folder for our application:

Looks good to me. HelloWorld is always my favorite application. Inside of this we are going to create a HelloWorld.rb file:

Okay, now lets put some Ruby code in this guy. All we want to do is write “Hello World” out to the console, so here it is:

puts "Hello World"

Pretty simple, huh? Now lets look at how we run this. First, get your console to the folder where you created this file:

Then we just run this simple command:

And that is it! You now have a working Ruby program.

So, you might be saying, okay so I have one file running. What if I want to make some classes in several files and run them? Well, I’m glad you asked! First, lets create a HelloWorld class.

class HelloWorld def say_hello puts "Hello World" end end

There, now we have a class that will write out our “Hello World” message. Being a C# guy, something is just very comforting about seeing the class there! Now, lets create a second file called “SayHello.rb” that contains this:

require "HelloWorld" hello = HelloWorld.new hello.say_hello

What you are seeing here is the equivalent of an import statement, and it is actually looking in the same folder as our “SayHello.rb” file and will find “HelloWorld.rb” so that we can reference the HelloWorld class. You can then see that we are creating a new instance of this class by calling the “new” method and then we call our “say_hello” method. (In case you are wondering, it is general convention to name ruby methods all lowercase with underscores separating words)

Now that we have all of this setup, we can then run it from the command line:

As you can see, we only need to specify the one file, the other one is found automatically.

So, there you have it, you now have the most basic skills needed to start writing Ruby applications. IronRuby is aimed at being a feature complete version of Ruby 1.8, so you should be able to go out there and use just about any Ruby tutorial to start developing in IronRuby. Over the next few days I am going to be experimenting with IronRuby and will try and post many of my findings up on the blog! I hope you enjoyed it!