Tuesday, May 31, 2011

IronRuby Tutorial, Ruby For C# programmers,Part 1

IronRuby is one of the DLR extension on Microsoft .net platform that enable you programming against .net using the Dynamic Language. I will put a very short tutorial, here.

  Download the assemblies or just the installer file from http://ironruby.codeplex.com/releases/view/49097#DownloadId=159561
for the installation, it basically puts several .net assemblies to the GAC folder. 

Then you can run the IronRuby console, which is the C# version of Irb
image

when you run process explorer, you will see the loaded Assemblies,
image
Ir.exe is a standard .net application,

image

Now, let’s write a basic HelloWorld Ruby application,
First in Ruby Style,
image

then In C#, we just call System.Console.WriteLine(“Message”). namespace will be mapped to module in Ruby,
image

Now, you can call the standard .net library, like Datetime
image

In Ruby, if you want to query the methods of the Object, you can use .methods, we can do this too.
image

if we want to change the Console foreground color, which we cant do in ruby easily,just call system.foreground_color
here, we can use ruby style naming like foreground_color, or C# style ForegroundColor, the dynamic method dispatcher will delver the call to the right stub,
image

To using the Base64 encoding that I used pretty often,
image

in Ruby, it’s easy to subclass to add more methods, say thing error here, I will add one method called writeerror

image

No comments:

 
Locations of visitors to this page