IronRuby Sample #1 – Hello World

A bunch of readers have asked me to post IronRuby samples. I took your advice and I am starting a series of posts where I’ll write some IronRuby samples so you can see what’s IronRuby code is all about.

I’ll begin with the simplest sample and as the series goes on, I’ll try to bring you more complex samples. If you’d like to see a specific sample, please contact me and let me know about it.

So the first sample is a Hello World sample. The regular Ruby Hello World app, which of course runs on IronRuby as well, is as follows:

puts "Hello World"

 

Now, the great thing about IronRuby is the ability to use .Net classes. The next code also writes Hello World to the console, but this time it uses .Net’s System.Console class. The sample also sets the color of the text to green:

orig_color = System::Console.foreground_color

System::Console.foreground_color = System::ConsoleColor.green
System::Console.write_line "Hello World"

System::Console.foreground_color = orig_color

 

To run that, just save this text into a file and execute it by ir.exe <file name>. This is how its execution will look like:

IronShay - IronRuby Sample #1

.Net developers pay attention - CLR objects get Ruby’s naming conventions when they are converted to IronRuby objects. This operation is called “name mangling”.  This is why System.Console.ForegroundColor appears as System::Console.foreground_color in IronRuby. You can read more about name mangling in the .Net Interoperability Fundamentals chapter in my book. These chapters are available for free through the Rough Cuts program.

If you’d like to see a certain sample next time, let me know.

All the best, 
Shay


Comments

October 26. 2009 06:33 PM

trackback

C# Recorder using IronRuby

C# Recorder using IronRuby

IronShay

September 23. 2011 05:07 PM

pingback

Pingback from freerob.com

HTML | Free Software | Free Templates | Freerob.com

freerob.com

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Subscribe Subscribe

That's Me!

Hi! I'm Shay Friedman
I'm Shay Friedman - a Visual C#/IronRuby MVP, a consultant and instructor of .NET technologies, author, speaker and new technologies freak
More about me

Contact Me

> Contact page
> Twitter: @ironshay
> LinkedIn profile

Search

Hosted By

I'm hosting this site on Arvixe and I'm very happy with it.
If you're looking for ASP.NET hosting, I highly recommend it
(and if you order from this link I also get some beer money!)
Web Hosting