This post was migrated from Justinโ€™s personal blog, 'Codethinked.com.' Views, opinions, and colorful expressions should be taken in context, and do not necessarily represent those of Simple Thread (and were written under the influence of dangerous levels of caffeination).

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:

image

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

image

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:

image

Then we just run this simple command:

image

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:

image

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!

10 Comments

Kevin Hazzard, MVP

Thanks, Justin. It would be nice to see a whole series of "Learning Ruby via IronRuby" posts. There’s a lot of information out there on IronRuby but not a lot of teaching material, if you know what I mean. This post as a step-by-step feel to it which would be good to see in a series. I’m a Python jockey but I’d love to learn Ruby by watching you.

Kevin

Reply
Justin Etheredge

Excellent idea, I may just have to do that! I think I might do it by comparing it to C#. This way all of the C# programmers out there can get started by seeing familiar syntax compared to the Ruby syntax. The challenge there is not writing Ruby like it is C#. ๐Ÿ™‚

Reply
Ryan Lanciaux

Justin, thank you this is a great series. I am really looking forward to more. It’s exciting that .NET is becoming very polyglot-ish ๐Ÿ™‚

Reply
Jason Olson

Wow, "polygot-ish fantasies". LOL, that’s the quote of the day right there :).

I love the direction you’re going with these Justin. Keep up the great blogging :).

Reply
quek

Hi,

Your RSS feed doesn’t seem to be there.
Like what you have written.

Thanks.

Reply
srinivasan

good tutorial

i was never been a ruby developer want to learn since i am primarily a vc++ developer now want to concentrate a lot on cloud computing and ironruby.

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *