CodeThinked

codethinked (kōdthĭngked) adj. To be consumed by or obsessed with code.

Running Applications in IronRuby

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!

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

Kevin Hazzard, MVP

July 17. 2008 10:39

United States
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#. Smile

Justin Etheredge

July 17. 2008 10:51

United States
pingback

Pingback from alvinashcraft.com

Dew Drop - July 17, 2008 | Alvin Ashcraft's Morning Dew

alvinashcraft.com

July 17. 2008 15:29

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 Smile

Ryan Lanciaux

July 17. 2008 16:22

United States
Justin Etheredge

@Ryan I'm happy to indulge your polyglot-ish fantasies Smile

Justin Etheredge

July 17. 2008 16:46

United States
Jason Olson

Wow, "polygot-ish fantasies". LOL, that's the quote of the day right there Smile.

I love the direction you're going with these Justin. Keep up the great blogging Smile.

Jason Olson

July 17. 2008 18:02

United States
Johan Danforth

Justin, I'm also learning (Iron)Ruby and did a couple of (somewhat chaotic) posts about basic Ruby stuff and compared it to c#/vb.net. Perhaps you want to peek at them and get a few ideas.

weblogs.asp.net/.../...y-looking-at-some-ruby.aspx
and
weblogs.asp.net/.../...ng-at-some-ruby-part-2.aspx

Cheers!
Johan

Johan Danforth

July 17. 2008 18:03

Sweden
Justin Etheredge

@Jason Ha, thanks!

@Johan I'll be sure to take a look at those. Thanks!

Justin Etheredge

July 17. 2008 18:16

United States
quek

Hi,

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

Thanks.

quek

July 18. 2008 05:34

Singapore
Justin Etheredge

Are you still not seeing it?

Justin Etheredge

July 19. 2008 02:03

United States
trackback

Trackback from CodeThinked

Learning Ruby via IronRuby and C# Part 1

CodeThinked

July 20. 2008 22:06

pingback

Pingback from rubyinside.com

A Great Set of IronRuby Tutorials To Bring C# Developers Into The Ruby Fold

rubyinside.com

July 23. 2008 21:51

pingback

Pingback from rubyworld.wordpress.com

Compare IronRuby with C#, Smile And Learn As Well. «

rubyworld.wordpress.com

July 25. 2008 08:30

trackback

Trackback from Paul van Brenk.com

Learning IronRuby

Paul van Brenk.com

August 7. 2008 08:44

pingback

Pingback from blog.mikeadmire.com

Mike Admire’s Blog  » Blog Archive   » Getting Started with IronRuby

blog.mikeadmire.com

August 8. 2008 19:21

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.

srinivasan

December 26. 2008 05:37

India
Comments are closed